; *********************************************************************** ; * * ; * Compare Two Numbers (First Version in Assembly Language) * ; * * ; *********************************************************************** ; Authors: Saeid Nooshabadi and John Zaitseff ; Date: 5th May, 2003 ; Version: 1.5 ; This program compares two integer numbers. The difference between this ; program and cmp-s.s is that this one uses a function, larger. Beware: ; there are some subtle bugs in this program, as explained in ELEC2041 ; Experiment 3. ; ----------------------------------------------------------------------- ; Assembly-language preamble for the main module .text ; Executable code follows _start: .global _start ; "_start" is required by the linker .global main ; "main" is our main program b main ; Start running the main program ; ----------------------------------------------------------------------- ; Start of the main program ; This function loads the variables "a" and "b", compares them and stores ; the larger of the two in the variable "c". The registers R0, R1 and R3 ; are used. main: ldr r3, =a ; Load R3 with the address of "a" ldr r0, [r3] ; R0 = 0xE0001234 (the value of "a") ldr r1, [r3,#4] ; R1 = 0x00004567 (NB: Address of "b" is R3+4) bl larger ; Call the function "larger"; store the return ; address (address of next instruction) in ; R14 (also called LR). Result returned in R0 ldr r3, =c ; Load R3 with the address of "c" str r0, [r3] ; Store result in "c" (0x00004567) exit: mov pc, lr ; Stop the program (Caution: bugs are present!) ; ----------------------------------------------------------------------- ; Function: int larger (int first, int second) ; This function calculates the larger of the two unsigned integers in ; registers R0 and R1. It returns the larger of the two in register R0. larger: cmp r0, r1 ; Compare "first" and "second" (cmp performs ; first - second, but does not store the ; result of a - b) movle r0, r1 ; Place R1 in R0 if and only if R0 ("first") ; <= R1 ("second") mov pc, lr ; Return to function main(): restore saved copy ; of LR into PC. ; ----------------------------------------------------------------------- ; Program variables (stored together with the code) a: .word 0xE0001234 ; Variable "a" b: .word 0x00004567 ; Variable "b" c: .word 0 ; Variable "c" ; ----------------------------------------------------------------------- .end