ARM汇编代写代做代考 CS1021 Tutorial 3

CS1021 Tutorial 3
Q1 Translate each of the following pseudo-code statements into a sequence of ARM assembly language instructions. Assume x and y are signed integers and x is in R1 and y is in R2.
(i) if (x== 0)
x = x + 5;
CMP R1,#0 BNE L1
ADD R1, R1, #5
L1 …
(ii) if (x >= 5) x = 0;
CMP R1, #5 BLT L1 MOV R1, #0
L1 …
(iii) x = 10; y = 5;
while (x > 0) { y = y*x;
x = x – 1;
}
L1 CMP BLE L2
MUL R2, R1, R2 SUB R1, R1, #1 B L1
L2 …
;x==0?
; != (opposite condition to == in pseudo-code) ; x = x + 5
;x>=5?
; < (opposite condition to >= in pseudo-code) ;x=0
MOV R1,#10 MOV R2, #5
;x=10
;y=5
;x==0?
; <= (opposite condition to > in pseudo-code) ; y = y*x
; x = x – 1
R1, #0
1
CS1021 Tutorial 3  2018 jones@scss.tcd.ie

(iv) if(x<9){ x = x + 1; } else { x = 0; } L1 MOV L2 ... (v) if (x > 9) { x = 0;
R1, #0
CMP R1,#9 BGE L1
ADD R1, R1, #1 B L2
;x<9? ; >= (opposite condition to < in pseudo-code) ; x = x + 1 ; skip else ;x=0 if (y > 9) { y= 0
} else {
y = y + 1;
}
} else {
x = x + 1; }
CMP R1, #9 BLE L2 MOV R1, #0 CMP R2, #9 BLE L1 MOV R2, #0 B L3
L2 ADD
L3 …
;x>9?
; <= (opposite condition to > in pseudo-code) ;x=0
;y>9?
; <= (opposite condition to > in pseudo-code) ;y=0
; skip else parts
; y = y + 1
; skip else part
; x = x + 1
L1 ADD
B L3
R2, R2, #1 R1, R1, #1
2
CS1021 Tutorial 3  2018 jones@scss.tcd.ie

Q2 Write an ARM assembly language program to compute xy where x and y are unsigned integers. Assume x is in R1, y in R2 and the result is stored in R0.
MOV R1, #2 MOV R2, #4 MOV R0, #1
;testwithx=3
;testwithy=4
;r=1
;while(y!=0)?
; == (opposite condition to != in pseudo-code) ;r=r*x
;y=y–1 ; repeat
L1 CMP BEQ L2
MUL R0,R1,R0 SUB R2,R2,#1 B L1
L2 …
R2, #0
3
CS1021 Tutorial 3  2018 jones@scss.tcd.ie