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 i,j,a,b,c,d;
a = 5;
b = 10;
c = 1;
d = 2;
for(i=0;i<=a;i++){
for(j=0;j<=b;j++){
c++;
}
d++;
}
}
assembly code :
MOV [sp], 5
MOV [sp+4], 10
MOV [sp+8], 1
MOV [sp+12], 2
MOV r0, 0
JMP L2
L3 :
MOV r1, 0
JMP L4
L5 :
/* something missing */
ADD r1, 1
L4 :
CMP r1, [sp+4]
/* something missing */
ADD r0, 1
L2 :
/* something missing */
JLE L3
MOV r0, [sp]
MOV r1, [sp+4]
MOV r2, [sp+8]
MOV r3, [sp+12]
Input
There is no input.
Output
Please print the complete assembly code and use minimal instructions.