; ; CPE 221 Assembly Example ; 3/6/18 ; This program multiplies two numbers by repeated addition. ; First, take absolute values of both numbers, do multiplication, ; then adjust result as necessary. AREA MULTIPLY_BY_ADDING, CODE, READONLY ENTRY LDR r0, num1 ; Put num1 in r0. LDR r1, num2 ; Put num2 in r1. MOV fp, #0x0000C000 MOV sp, #0x00000000 ADR r10, case1 mpy_ne MOV r3, #0 ; Set r3 to 0, it will hold the result. TEQ r0, #0 ; Compare first parameter to 0 BEQ done ; If first parameter is 0 we are done, result = 0. TEQ r1, #0 ; Compare second parameter to 0 BEQ done ; If second parameter is 0, we are done, result = 0. PUSH {fp} ; Save old frame pointer in preparation for branching MOV fp, sp ; Copy stack pointer into the frame pointer SUB sp, sp, #8 ; Make space on the stack for one input, one output STR r1, [fp, #-4] ; Store the input parameter on the stack BL abs ; Branch to abs routine and store return in lr LDR r4, [fp, #-8] ; Copy result from the stack into r4 SUB sp, sp, #8 ; Make space on the stack for one input, one output STR r0, [fp, #-4] ; Store the input parameter on the stack BL abs ; Branch to abs routine and store return in lr LDR r5, [fp, #-8] ; Copy result from the stack into r5 MOV sp, fp ; Collapse the frame by moving the stack pointer POP {fp} ; Pull the old frame pointer off the stack adding ADD r3, r3, r5 ; Add num2. SUBS r4, r4, #1 ; Decrement r4, the abs of num1. BEQ adjust ; If r4 = 0, done adding, go to adjust. B adding ; Otherwise, need to add again. adjust MOVS r0, r0 ; Done adding, now adjust sign of result. RSBMI r3, r3, #0 ; If num2 negative, negate result. MOVS r1, r1 RSBMI r3, r3, #0 ; If num1 negative, negate result. done STR r3, [r10] ; Store the result final B final abs LDR r9, [fp, #-4] ; Copy the input parameter off of the stack CMP r9, #0 ; Test input parameter BPL d_abs ; If zero or greater, we're done RSB r9, r9, #0 ; If negative, make it positive d_abs STR r9, [fp, #-8] ; Store the result on the stack MOV pc, lr ; Pull the saved lr value off stack and put in pc to return num1 DCD -3 ; Give num1 a value num2 DCD 2 ; Give num2 a value case1 SPACE 4 ; Make space for result END