Example of a assembly program for
Computer Organization Class
[ b a
c k ]
;////////////////////////////Test Driver//////////////
; This program includes the division function as well |
; as a driver to test the function. The driver should |
; also be used as a template for implimentation in |
; other programs as R28 and R29 will be set to their |
; values previous to the function call. This program |
; needs to be run in conjunction with the aghar |
; assemblier and linker. |
; |
; Author: William Martin |
; |
; Date: 3/2/2000 |
; |
; Input: Numerator (Register 28) and denominator |
; (Register 29). [keyboard in this driver] |
; Output: Register 30 (answer of div. funct.) |
;/////////////////////////////////////////////////////
.bss
num: .reserve 4 ;to reset regs. later
den: .reserve 4
.text
INPUT R28 ;get numerator
INPUT R29 ;get denominator
STORE.w num, R28 ;save reg values
STORE.w den, R29
BL R31, divide ;call to function
NOP
DUMP R30 ;put answer
LOAD.w R28, num ;reset registers
LOAD.w R29, den
NOP
STOP
NOP
;////////////////////////////Divide Function/////////
; This function will provide a division operation. |
; Call this function with "BL R31, divide" |
; |
; Author: William Martin |
; |
; Date: 3/5/2000 |
; |
; Preconditions: Numerator in Register 28 and |
; Denominator in Register 29 |
; Postconditions: Answer in register 30, if zero |
; denominator than program will quit |
; with a error value in register 30 |
;/////////////////////////////////////////////////////
.data
.equate error, 2147483646 ;max value
.bss
temp: .reserve 4 ;to store R1
.text
divide: MOVE R30, R0 ;initialize answer
STORE.w temp, R1 ;save R1 value
MOVE R1, R0 ;initialize flag
BREQ R29, R0, err ;if 0 den. than error
test_num: BRGT R28, #0, test_den ;if neg. than flag
NOP
NEG R28, R28 ;make absolute value
ADD R1, #1, R1 ;flag
test_den: BRGT R29, #0, loop_top ;if neg. than flag
NOP
NEG R29, R29 ;make absolute value
SUB R1, #1, R1 ;flag, if flagged, unflag
loop_top: BRLT R28, R29, bottom ;while num<den loop
SUB R28, R28, R29 ;num - den
ADD R30, R30, #1 ;answer + 1
BRANCH loop_top
NOP
bottom: BREQ R1, R0, done ;if no flag, done
NOP
NEG R30, R30 ;if flag, ans is - val.
done: LOAD.w R1, temp ;reset register 1
BRANCH R31 ;return to function caller
NOP
err: MOVE R30, #error ;catch zero_denominator
DUMP R30 ;return error value
STOP ;halt program
NOP
.end