Question: Modify the code bellow so that it now has a main and a separate function largest that take 3 parameters (r0, r1, r2) and returns

Modify the code bellow so that it now has a main and a separate function largest that take 3 parameters (r0, r1, r2) and returns the largest of the three in r4. The main will load these values from memory, call the function and then store the result back into memory.

As this function is a leaf function, and should only use R0-R3, it can be written more simply than recursive example in part 1 as it will not need to save and restore values from the stack.

The code:

AREA example, CODE, READONLY

ENTRY

; load the values from memory

LDR R1, =N1 ; load N1 into R1

LDR R2, =N2 ; load N2 into R2

LDR R3, =N3 ; load N3 into R3

; compare the values in R1 and R2

CMP R1, R2 ; compare R1 and R2

BGE check_R1_R3 ; jump to check_R1_R3 if R1 >= R2

B check_R2_R3 ; jump to check_R2_R3 if R1 < R2

check_R1_R3:

; compare the values in R1 and R3

CMP R1, R3 ; compare R1 and R3

BGE set_R0_R1 ; if R1 >= R3, set R0 to R1

MOV R0, R3 ; otherwise, set R0 to R3

B store_result ; jump to store_result

check_R2_R3:

; compare the values in R2 and R3

CMP R2, R3 ; compare R2 and R3

BGE set_R0_R2 ; if R2 >= R3, set R0 to R2

MOV R0, R3 ; otherwise, set R0 to R3

B store_result ; jump to store_result

set_R0_R1:

; set R0 to the value in R1

MOV R0, R1 ; set R0 to R1

B store_result ; jump to store_result

set_R0_R2:

; set R0 to the value in R2

MOV R0, R2 ; set R0 to R2

B store_result ; jump to store_result

store_result:

; store the result in memory

LDR R4, =RESULT ; load RESULT label into R4

STR R0, [R4] ; store R0 at the address pointed to by R4

end:

; exit the program

BX LR ; return from the function

; data section

AREA data, READWRITE

N1 DCD 34

N2 DCD 54

N3 DCD 21

RESULT DCD 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!