I\'m trying to find the largest number of three in ARM assembly language. I alre
ID: 3723370 • Letter: I
Question
I'm trying to find the largest number of three in ARM assembly language. I already have some code from a previous "finding the largest number" question.
For this one, I want to modify my code so that it LOADS the number values from MEMORY into the registers. Use a label and a .data block to put these into three consecutive memory locations. Then store the answer into a fourth memory location immediately following the three original values; use a second labe lwith a .skip directive to setup this location.
/* -- ASML8_1.s */
.text
.global
main:
STR R1, #43 ; 43
STR R2, #54 ; 54
STR R3, #21 ; 21
STR R0, #0
SUBS R0, R1, R2
BLT less_than
BGT greater_than
less_than
MOV R0, R2
SUBS R4, R2, R3
BLT secondLess
BGT secondGreat
secondLess
MOV R0, R3
END
secondGreat
END
greater_than
MOV R0, R1
SUBS R5, R1, R2
BGT last
BLT secondLast
last
END
secondLast
MOV R0, R2
END
Explanation / Answer
.text
.global
main:
; Load all the three numbers from memory
LTR R1, [0002] ; assuming first number at memory location 0002
LTR R2, [0004] ; if numbers are integer, successive numbers will be at offset 4
LTR R3, [0008]
MOV R0, #0 ; register which will contain largest number
SUBS R0, R1, R2 ; compare R1 & R2 , R0=R1-R2
BLT less_than ; if R1<R2
BGT greater_than ; if R1>R2
less_than:
MOV R0, R2 ; since R2>R1, temporarily store R2 into R0
SUBS R4, R2, R3 ; Now compare R2 & R3
BLT secondLess ; if R2<R3 => R3 is largest
BGT secondGreat ; if R2>R3 => R2 is largest
secondLess:
MOV R0, R3 ; update R0 to R3
END
secondGreat:
END ; no need to update R0, coz R2 is already stored in R0
greater_than
MOV R0, R1 ; since R1>R2, temporarily store R1 into R0
SUBS R5, R1, R3 ; Now compare R1 & R3
BLT last ; if R1<R3 => R3 is largest
BGT secondLast ; if R1>R3 => R1 is largest
last
MOV R0, R3 ; update R0 to R3
END
secondLast
END ; no need to update R0, coz R1 is already stored in R0
; Sotre largest number to subsequent memory location
STR R0, [000C]