Assembly Programming Exercises
Exercise 1
Write a program (div.asm) to perform a positive integer long-division algorithm. Your program will have two inputs: the dividend and divisor and have two outputs: the quotient and remainder.
For simplicity, assume, you will be given only positive values and the divisor will be always greater than zero. HINT: For division, use the repeated subtraction method i.e. subtract the divisor from the dividend till your result becomes zero. In some cases, you will have remainder though.
To grade the program, your inputs and outputs are as follows:
Register R1 = Divisor
Register R6 = Dividend
Register R2 = Quotient
Register R3 = Remainder.
Example: R1 = 50, R6 = 105. Therefore 105/50, R2 = 2 and R3 = 5.
Exercise 2
Write a program that takes the string Lastname, firstname. The string is located at location FLNAME. To initialize the constant string we use the.STRINGZ directive which initializes locations with ASCII value of the character in the sequence. Note that.STRINGZ also null terminate the string. Your program will display the string as-is, then your program must display the first name first followed by a space followed by the last name.
Change the characters after.STRINGZ instruction in the file to test different cases.
You need to turn in your .asm code and a screen print of the output. Here is a sample of the output:
The original full name is:
Mansour, Khaled
The modified full name is:
Khaled Mansour
Solution:
1.
.ORIG x3000 ; Program start address
LD R1, DIVISOR ; Load divisor
LD R6, DIVIDND ; Load dividend
; At the start:
; R1 = Divisor
; R6 = Dividend
; At the end:
; R2 = Quotient
; R3 = Remainder
AND R2, R2, #0 ; Start quotient to zero
ADD R3, R6, #0 ; Copy dividend, it will be the initial remainder
DIVLOOP NOT R5, R1 ; take ones complement of divisor
ADD R5, R5, #1 ; add 1 to get the two's complement (negative)
ADD R5, R5, R3 ; subtract remainder - divisor
BRn DONE ; if result is negative, end division
ADD R3, R5, #0 ; Copy result to current remainder
ADD R2, R2, #1 ; increment quotient
BR DIVLOOP ; repeat division loop
DONE BR DONE ; Infinite loop to stop program
DIVISOR .FILL #50 ; Divisor
DIVIDND .FILL #105 ; Dividend
.END
Output:
2.
.ORIG x3000 ; Program start address
LEA R0, ORIGMSG ; Load original string address
PUTS ; print original string message
LD R0, LF ; load line feed character
OUT ; jump to next line
LEA R0, FLNAME ; Load fullname string address
PUTS ; print fullname string
LD R0, LF ; load line feed character
OUT ; jump to next line
LEA R1, FLNAME ; Load fullname string address
LD R2, NCOMMA ; load negative of comma
FNDLOOP LDR R3, R1, #0 ; Load character from string
ADD R4, R3, R2 ; compare with comma
BRz DISPLAY ; if equal, print strings
ADD R1, R1, #1 ; advance to next char in string
BR FNDLOOP ; repeat loop
DISPLAY STR R4, R1, #0 ; save null to replace comma and end string
LEA R0, MODMSG ; Load modified string address
PUTS ; print modified string message
LD R0, LF ; load line feed character
OUT ; jump to next line
ADD R0, R1, #2 ; point to space after the comma and the space
; which is the start of the first name
PUTS ; print first name string
LD R0, SPACE ; load space character
OUT ; display space
LEA R0, FLNAME ; Load last name string address
PUTS ; print last name string
LD R0, LF ; load line feed character
OUT ; jump to next line
DONE BR DONE ; Infinite loop to stop program
FLNAME .STRINGZ "Mansour, Khaled" ; Original full name
ORIGMSG .STRINGZ "The original fullname is:" ; Original message
MODMSG .STRINGZ "The modified fullname is:" ; Modified message
NCOMMA .FILL #-44 ; negative of ascii value for comma ','
SPACE .FILL 32 ; space character
LF .FILL 10 ; line feed to jump to next line
.END
Output: