0

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

4
Contributors
7
Replies
83
Views
2 Months
Discussion Span
Last Post by Carolyn_3
1

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.

0

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
load 0001 1 load memory content into accumulator
store 0010 2 store contents of accumulator in memory
read 0011 3 read a value into accumulator
write 0100 4 write from accumulator to ouptut
add 0101 5 add contents of accumulator to specified memory address
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
jmp 1001 9 jump to a location
jmpz 1010 10 if acc = 0 jump to a location
jmpn 1011 11 if acc < 0 jump to a location

Thanks
Jed

0

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:
          read                     ; a = input
          jmpz     END             ; if a == 0, goto end
          store    NUM             ; num = a
          read                     ; a = input
          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
          load     NUM
          sub      TMP
          store    D1
          load     TMP
          div      BASE
          store    NUM
          jmpz     PRN_D1

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

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

          stop                      ; overflow

PRN_D3:
          load     D3
          write
PRN_D2:
          load     D2
          write
PRN_D1:
          load     D1
          write
PRN_D0:
          load     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
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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.