10446 - hw3 - code translation 2   

Description

We have 4 normal registers, r0, r1, r2, r3, whose initial values are all zeros, and one spe- cial register, sp, which stores stack pointer. Be careful, we cannot change the value in sp register. This CPU has 5 kinds of instructions, MOV, ADD, CMP, JMP, JLE, whose formats are specified as follows.

 
: Any register, r0, r1, r2, r3
: A memory address. For example, [sp+8]. Be careful, there is no space between ‘[’ and ‘]’.
: A constant value
 
MOV , will copy the value stored in the latter to the former
 
MOV ,
 
MOV ,
 
MOV ,
 
MOV ,
 
ADD , will add the value stored in the latter to the former
 
ADD ,
 
ADD ,
 
ADD ,
 
ADD ,
 
CMP ,
 
CMP ,
 
CMP ,
 
CMP ,
 
JMP
 
JLE  
 
Please fill in the following assembly code which is a translation of the following c code using these introduced instructions and store variable “a” in r0, variable “b” in r1, variable "c" in r2, variable "d" in r3.

c code : 
int main(){
int a=10, b=20, c=30, d=40;
while(a<=d){
if(b<=c){
  a++;
b++;
}
else if(a<=b) a++;
else a+=2;
}
}

assembly code : 

MOV r0, 10
MOV r1, 20
MOV r2, 30
MOV r3, 40
JMP L2
L3 :
CMP r1, r2
/* something missing */ 
CMP r0, r1
JLE L5
ADD r0, 2
JMP L2
L5 :
/* something missing */
JMP L2
L4 :
ADD r0, 1
ADD r1, 1
L2 :
/* something missing */

Input

 There is no input.

Output

 Please print the complete assembly code and use minimal instructions.

Sample Input  Download

Sample Output  Download

Tags




Discuss