IN ASSEMBLY LANGUAGE For this program, you will do addition and subtraction with
ID: 3808342 • Letter: I
Question
IN ASSEMBLY LANGUAGE
For this program, you will do addition and subtraction with quad words. These are declared using QWORD. These data items are 8 bytes in length, rather than 4 bytes for a DWORD. They are storded in little endian order (see slide 61 in SS3). An example is:
DATA1 QWORD 0100000000000001h
To do addition of QWORDs, you need to first add the least significant 4 bytes and then add the most significant 4 bytes of each operand. There may be a carry from the least significant addition to the most significant addition. This is dealt with using the ADC instruction. This instruction adds both the source operand and the carry flag to the designation operand. As with all instructions, the source and designation operands must be the same size.
To do subtraction of QWORDs, you need to first subtract the least significant 4 bytes and then subtract the most significant 4 bytes of each operand. There may be a borrow from the least significant subtraction to the most significant subtraction. This is dealt with using the SBB instruction. This instruction subtracts the source operand from the desination operand thd then subtracts the carry flag from the destination.
You will declare 4 data values:
DATA1 QWORD 01000000FF000001h
DATA2 QWORD 02000000FF000002h
DATA3 QWORD ?
DATA4 QWORD ?
Your program will have two procedures: AddQwords and SubQwords which are called in main to do the following two calculations:
DATA3 = DATA1 + DATA2, and
DATA4 = DATA3 - DATA2
Note that for the second calculation, the modified value of DATA3 is used.
Use offsets for DATAx (stored in ESI, EDI and EBX) prior to calling the two procedures.
You must used a loop in both AddQwords and SubQwords to do the two additions and subtractions required. Be aware that when you do ANY add or subtract, the carry flag is modified.
You will use Irvine library functions WriteString, WriteHex, and CrLf to generate the following output (exactly):
Result of add is: 03000001FE000003
Result of subtract is: 01000000FF000001
Explanation / Answer
INCLUDE Irvine32.inc;
.data
DATA1 QWORD 01000000FF000001h
DATA2 QWORD 02000000FF000002h
DATA3 QWORD 0
DATA4 QWORD 0
DUPPER DWORD 0
DLOWER DWORD 0
QVAL QWORD 0
addMsg db "Result of add is: ", 0xA,0xD
subMsg db "Result of subtract is: ", 0xA,0xD
.code
main PROC
;add
mov edx, DWORD PTR[DATA2]
mov eax, DWORD PTR[DATA1]
sub eax, edx
mov DWORD PTR [DLOWER], eax
MOV DWORD PTR[QVAL], eax
mov edx, DWORD PTR[DATA2+4]
mov eax, DWORD PTR[DATA1+4]
add DWORD PTR eax, edx
mov DWORD PTR[QVAL+4], eax
mov DWORD PTR[DUPPER], eax
mov eax, DUPPER
call WriteHex
mov eax, DLOWER
call WriteHex
mov DATA3, DUPPER
call WriteHex
mov DATA3, DLOWER
call WriteHex
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, addMsg
mov edx, DATA3
int 0x80
;subtract
mov edx, DWORD PTR[DATA3]
mov eax, DWORD PTR[DATA1]
sub eax, edx
mov DWORD PTR [DLOWER], eax
MOV DWORD PTR[QVAL], eax
mov edx, DWORD PTR[DATA2+4]
mov eax, DWORD PTR[DATA1+4]
sbb DWORD PTR eax, edx
mov DWORD PTR[QVAL+4], eax
mov DWORD PTR[DUPPER], eax
mov eax, DUPPER
call WriteHex
mov eax, DLOWER
call WriteHex
mov DATA4, DUPPER
call WriteHex
mov DATA4, DLOWER
call WriteHex
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, subMsg
mov edx, DATA4
int 0x80
exit
main ENDP
END main