Converting Assembly language program to Little Man Computer(LMC), for exam revision

StackOverflow https://stackoverflow.com/questions/23573029

  •  19-07-2023
  •  | 
  •  

Question

(b) Consider the following assembly language program:

.data          
.text
.globl main
main:
    li $v0, 5                 # service #5 – input an integer 
    syscall                   
    move $t0, $v0             
    li $v0, 5                 
    syscall                    
    move $t1, $v0            
    sub $t2, $t1, $t0         # [t2]<- [t1]-[t0]
    bgtz $t2, BRCH          # branch to BRCH if [t2]>0
    sub $a0, $t0, $t1
    b CNT                     # branch to CNT
BRCH: sub $a0, $t1, $t0
    b CNT                    
CNT:    li $v0, 1                 # service #1 – print integer
    syscall                   
    li $v0, 10                # service #10 – exit system
    syscall                   
This program outputs the positive difference between the two inputted integer numbers.

(iii) Convert this program into an equivalent Little Man Computer (LMC) program using the following instructions:
[6 marks]

FORMAT   MEANING
000 Stops the Computer - the Little Man rests.
1xx Adds the contents of mailbox xx to the calculator display.
2xx Subtracts the contents of mailbox xx from the calculator display.
3xx Stores the calculator value into mailbox xx.
4xx Stores the address portion of the calculator value (last 2 digits) into the address portion of the instruction in mailbox xx.
5xx Loads the contents of mailbox xx into the calculator.
6xx This instruction sets the instruction counter to the number xx, thus effectively branching to mailbox xx 
7xx IF the calculator value is zero, THEN set the instruction counter to the number xx, thus effectively branching to mailbox xx. 
8xx IF the calculator value is positive (or zero), THEN set the instruction counter to the number xx, thus effectively branching to mailbox xx. 
901 Read a number from the IN basket and key it into the calculator.
902 Copy the number in the calculator onto a slip of paper and place it into the OUT basket.



Mailbox     Code        Instruction Description
00      901      INPUT (1st number input)
01      399      STO 99 (to mailbox no 99)
02      901      INPUT (2nd number input)
03      398      STO 98 (store to mailbox no 98)
04      299     SUB 99 (subtract 1st number from 2nd)
05      808     BRANCH 09 (Branch to 09 if 2nd > 1st ) 
06      599     LOAD 99 (load 1st)
07      298     SUB 99 (subtract 2nd  number from 1st)
08      902     OUTOUT (output) 
09      000     STOP (The Little Man rests)

98              DATA2 (2nd number)
99              DATA1 (1st number)

I have posted the question and the solution, I can't seem to understand the solution, can someone explain me it please?

Thanks

Was it helpful?

Solution

This:

FORMAT MEANING 000 Stops the Computer - the Little Man rests. 1xx Adds the contents of mailbox xx to the calculator display. 2xx Subtracts the contents of mailbox xx from the calculator display. 3xx Stores the calculator value into mailbox xx. 4xx Stores the address portion of the calculator value (last 2 digits) into the address portion of the instruction in mailbox xx. 5xx Loads the contents of mailbox xx into the calculator. 6xx This instruction sets the instruction counter to the number xx, thus effectively branching to mailbox xx 7xx IF the calculator value is zero, THEN set the instruction counter to the number xx, thus effectively branching to mailbox xx. 8xx IF the calculator value is positive (or zero), THEN set the instruction counter to the number xx, thus effectively branching to mailbox xx. 901 Read a number from the IN basket and key it into the calculator. 902 Copy the number in the calculator onto a slip of paper and place it into the OUT basket.

is not part of the LMC-code. It's just description of the opcodes.

main:
    ; INPUT (1st number input)
    li $v0, 5                 # service #5 – input an integer 
    syscall

    ; STO 99 (to mailbox no 99)
    move $t0, $v0  

    ; INPUT (2nd number input)
    li $v0, 5                 
    syscall

    ; STO 98 (store to mailbox no 98)                    
    move $t1, $v0  

    ; SUB 99 (subtract 2nd  number from 1st) - different subtraction order
    sub $t2, $t1, $t0         # [t2]<- [t1]-[t0]

    ; BRANCH 09 (Branch to 09 if 2nd > 1st ) - maybe should be BRANCH 08?
    bgtz $t2, BRCH          # branch to BRCH if [t2]>0

    ; move result in "accu" (subtract again)
    sub $a0, $t0, $t1

    ; continue to "08" using another branch (why?)
    b CNT                     # branch to CNT

    ; LOAD 99 (load 1st)
    ; SUB 99 (subtract 2nd  number from 1st) - should be SUB 98
BRCH: sub $a0, $t1, $t0

    ; continue to "09" (why?)
    b CNT

    ; OUTOUT (output)                    
CNT:    li $v0, 1                 # service #1 – print integer
    syscall                   
    li $v0, 10                # service #10 – exit system
    syscall                   

Like you see, the LMC code has errors, but basically here I commented the MIPS code with more or less equivalent pieces of LMC-code. Also both codes could be written somewhat better.

Does that help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top