Hi guys.

My task is to write a wombat 1 machine language program in CPUSIM that outputs the radix(only base 2 to 8 inclusive) reperesentation of a number. We have to ask the user for 2 inputs - first is the value to convert(cannot be negative) and second is the radix base. Then do division and note each remainder and display in the correct order. We were also told that not all values will be able to be converted and that we have the choice of how many spaces to set aside for the remainders.

I've managed to get a working program but can't figure out displaying remainders in the correct order. Currently, my program does not store the remainder. As soon as it is calculated, it is displayed. But the instructions specifically state to display in correct order. My division part and remainder calculation is in a loop which means that if I create a storage space for the remainder, it will be overriden on each loop. So I thought of jumping out of the loop to another location and storing it in a permanent space. Then comes the problem of the other remainders.

If you want my code, pm me please. I'll send it as a private message. Sorry if i'm not posting it up, but this is an assignment question and last time I posted my code on a forum, another classmate found it, copied it, and we both got zero for plagiarism. Please understand.

Really appreciate it if someone could help me out.

Thanks
Jed

## All 7 Replies

You could use the stack. Push the digits as you generate them. Then in a second loop, pop the digits and print them. You need to know when you've popped the last digit which you can either do with a counter or a sentinel value. Here's some pseudocode that uses a sentinel.

``````// n is number, b is base

push 255    ; sentinel marking top of stack

10:
r = n % b              ; remainder
push r
n = n / b
if n != 0 goto 10

20:
pop d                  ; digit
if d == 255 goto 30
if d > 9 goto 25
print d + '0'
goto 20
25:
print d + ('A' - 10);
goto 20

30:  end``````

Alternatively, you could use a block of memory as an array, store the digits there at increasing indices, then print them backwards.

Thanks for the suggestion but I cannot use that. My lecturer said so. He said to use memory spaces for remainder storage. We had to decide how many. And although it's not the most efficient program, it's what is required.

These are the only instructions for wombat 1 that we've been given to use:

stop 0000 0 stop execution
store 0010 2 store contents of accumulator in memory
write 0100 4 write from accumulator to ouptut
sub 0110 6 subtract contents of a memory address from accumulator
mult 0111 7 mult acc with another memory address content
div 1000 8 divide acc content with mem address content

Thanks
Jed

In that case all I can think of is the following. I've only coded it for 4 digits maximum but you may want to expand it to 16 (or how many you think you need).

``````LOOP:
jmpz     END             ; if a == 0, goto end
store    NUM             ; num = a
store    BASE            ; base = a
load     NUM             ; a = num

div      BASE            ; a /= base
mult     BASE            ; a *= base
store    TMP             ; tmp = a
load     NUM             ; a = num
sub      TMP             ; a -= tmp
store    D0              ; d0 = a
load     TMP             ; a = tmp
div      BASE            ; a /= base
store    NUM             ; num = a
jmpz     PRN_D0          ; if a == 0, goto prn_d0

div      BASE            ; ditto for D1
mult     BASE
store    TMP
sub      TMP
store    D1
div      BASE
store    NUM
jmpz     PRN_D1

div      BASE            ; ditto for D2
mult     BASE
store    TMP
sub      TMP
store    D2
div      BASE
store    NUM
jmpz     PRN_D2

div      BASE            ; ditto for D3
mult     BASE
store    TMP
sub      TMP
store    D3
div      BASE
store    NUM
jmpz     PRN_D3

stop                      ; overflow

PRN_D3:
write
PRN_D2:
write
PRN_D1:
write
PRN_D0:
write

jmp      LOOP
END:
stop

NUM:      .data    2   0
BASE:     .data    2   0
TMP:      .data    2   0
D0:       .data    2   0
D1:       .data    2   0
D2:       .data    2   0
D3:       .data    2   0``````

Hi, if you still need help send me your code. My email address is maxdre224@gmail.com, I'll have a look at it and see how I can improve it. Shouldn't be too hard.
Cheers.

Thanks Max. I've sent an email

is there another way to shorten the wombat code so as to not have too many lines for i need to put 8 bits

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.