将汇编语言程序转换为Little Man Computer(LMC),以便进行考试修订

时间:2014-05-09 20:01:58

标签: assembly

(b)考虑以下汇编语言程序:

.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)将此程序转换为等效的小型计算机(LMC)       程序使用以下说明:
        [6分]

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)

我已经发布了问题和解决方案,我似乎无法理解解决方案,有人可以解释一下吗?

由于

1 个答案:

答案 0 :(得分:0)

此:

  

FORMAT MEANING 000停止计算机 - 小人休息。 1XX   将邮箱xx的内容添加到计算器显示中。 2XX   从计算器显示中减去邮箱xx的内容。 3XX   将计算器值存储到邮箱xx中。 4xx存储地址   计算器值的一部分(最后2位数)到地址中   邮箱xx中指令的一部分。 5xx加载内容   邮箱xx进入计算器。 6xx该指令设置   指令计数器到数字xx,从而有效地分支到   mailbox xx 7xx如果计算器值为零,则设置为   指令计数器到数字xx,从而有效地分支到   邮箱xx。 8xx如果计算器值为正(或零),那么   因此,有效地将指令计数器设置为数字xx   分支到邮箱xx。 901从IN篮子和钥匙中读取一个数字   它进入计算器。 902将计算器中的数字复制到a上   纸条并将其放入OUT篮子。

不是LMC代码的一部分。它只是对操作码的描述。

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                   

就像你看到的那样,LMC代码有错误,但基本上我在这里评论了MIPS 具有或多或少等效的LMC代码片段的代码。 这两个代码也可以写得更好。

这有帮助吗?