MIPS Assembly Code Write a function add64 that adds two input unsigned 64 bit in
ID: 2246603 • Letter: M
Question
MIPS Assembly Code
Write a function add64 that adds two input unsigned 64 bit integers x and y and returns the unsigned 64 bit integer sum z, i.e. z = x + y. In your main function, you should assume that x, y, and z will be stored in the following 6 registers as follows: x: upper 32 bits in $t1 lower 32 bits in $t0 y: upper 32 bits in $t3 lower 32 bits in $t2 z: upper 32 bits in $t5 lower 32 bits in $t4 Your main function call should read the values of x and y into the stack, invoke add64, and then return z on the stack. Also, your function should generate an error only if there is an overflow in the overall 64 bit addition (and not if there is an overflow in the addition of the lower 32 bit components). Hint: an overflow in the lower 32 bit addition means that there is a carry of 1 to the upper 32 bit addition. Only an overflow in the upper 32 bit addition means that there is a true overflow. One way you can implement this functionality is to have a “flag” register that is set when an overflow causes an interrupt, which will tell you that you have to “carry” a 1.
Explanation / Answer
.MODEL small
.STACK 100h
.DATA
op1 dd 12345678h
op2 dd 11111111h
ans dd ?
.CODE
.startup
mov ax, @data
mov ds, ax
mov ax, word ptr op1 ; lsb of number1 in ax
mov bx, word ptr op1+2 ; msb of number1 in bx
mov cx, word ptr op2 ; lsb of number2 in cx
mov dx, word ptr op2+2 ; msb of number1 in dx
add ax, cx ; add msb + msb + carry
mov word ptr ans, ax ; lsb answer
mov word ptr ans+2, bx ; msb answer
mov bx, word ptr ans+2 ; Result in reg bx
mov dh, 2
l1: mov ch, 04h ; Count of digits to be displayed
mov cl, 04h ; Count to roll by 4 bits
l2: rol bx, cl ; roll bl so that msb comes to lsb
mov dl, bl ; load dl with data to be displayed
and dl, 0fH ; get only lsb
cmp dl, 09 ; check if digit is 0-9 or letter A-F
jbe l4
add dl, 07 ; if letter add 37H else only add 30H
l4: add dl, 30H
mov ah, 02 ; INT 21H (Display character)
int 21H
dec ch ; Decrement Count
jnz l2
dec dh
cmp dh, 0
mov bx, word ptr ans ; display lsb of answer
jnz l1
mov ah, 4ch ; Terminate Program
int 21h
END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.