+ 1

question how do I add this R0 <- c ?

Question: a = 5; b = 10; c; //Create the assembly code to translate the following if statement if ( a <= b ) { c = a + b -3; } R0 <- c AREA | .text | , CODE, READONLY EXPORT __main a EQU 0x20000000 b EQU 0x20000004 c EQU 0x20000008 __main ; a = 5 MOVS R0, #5 LDR R7, =a ;first address in SRAM STR R0, [R7] ;b = 10 MOVS R1, #10 LDR R7, = b STR R1, [R7] ; if a<=b ; then c = a+b -3 LDR R7, = a LDR R0, [R7] CMP R0, R1 ; if R0 is equal or less ; if it flags then jump if not do the condition c = a + b -3; BNE endif1 ; branch not equal ;write the condition LDR R7, =a ; load the value of a LDR R0, [R7] LDR R7, =b ; load the value of b LDR R1, [R7] ADDS R0, R1 ; a+b SUBS R0, #3 ; (a+b) - 3 LDR R7, =c ; store result in c STR R0, [R7] endif1 done B done END

27th Sep 2020, 3:38 AM
arta farshadi
arta farshadi - avatar
1 Answer
+ 1
It should already end with c in R0, but there is a logic error that causes the program to skip the calculation and exit with 5 in R0. Fix this conditional: BNE endif1 ;branch not equal It should be:   BGT endif1    ; branch if greater than Also, it is unnecessary to reload variable a into R0 right before the CMP because R0 still holds the value 5 from when it stored it in a. So, LDR R7, = a LDR R0, [R7] CMP R0, R1 can be reduced to CMP R0, R1 Likewise, for the addition of (a+b), the values are already in registers R0 and R1, respectively. I think you can cut out the four LDR instructions before the ADDS R0, R1 instruction. To ensure R0 holds c for different a and b input values wherein execution skips the calculation, insert LDR R7, =c and LDR R0, [R7] after label endif1. If there is no further requirement to use a, b, and c then ultimately you could eliminate their allocated variable space and remove all LDR R7,* and STR instructons. All calculations would be done in the registers only.
27th Sep 2020, 12:27 PM
Brian
Brian - avatar