Write a program that calculates the expression Res = (A + B) – (C + D), using re
ID: 3743434 • Letter: W
Question
Write a program that calculates the expression Res = (A + B) – (C + D), using registers and variables. Several steps are suggested:
In the data segment, define variables ordered by: varA, varB, varC, varD, and Res with some integers initialized
In the code segment
calculate (A + B) – (C + D)
save the result in Res
You can copy the following to fill the blanks with your implementations. Please do not use more than six instructions.
- If you can use registers, don’t use memory variables. E.g., if available, use EAX,EBX,ECX,EDX, ESI,EDI, instead of creating var1, var2, etc..
- If possible, use less instructions (no more than 6 instructions), E.g., for
MOV ebx, var1
ADD eax, ebx
simply
ADD eax, var1
- Use an instruction with less bytes. E.g., both can move EAX value to var1
MOV var1, eax
XCHG var1, eax
If only want to move, logically use MOV, also because here MOV takes 5 bytes machine code but XCHG 6.
- If you can use symbolic constant, don’t use memory variable. E.g., for make an array count for
ary BYTE 1,2,3,4,5,6,7,8,9
use constant
aryCount = ($ - myArray)
don’t use variable
aryCount BYTE ($ - myArray)
When finished, test your program in debugger. If you define DWORD varA as 10, varB 20, varC 30, and varD 40, Res should be -40
---------------------- CODE ----------------------
.386 .model flat,stdcall .stack 4096 ExitProcess proto,dwExitCode:dword .data ; define variables: varA, varB, varC, varD, and Res ; ... ... .code main1 proc ; calculate Res = (A + B) – (C + D) ; ... ... ; calculate (A + B) – (C + D) ; ... ... ; save the result in Res invoke ExitProcess,0 main1 endp end main1
Explanation / Answer
---------------------- CODE ----------------------
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
; define variables: varA, varB, varC, varD, and Res
; ... ...
varA DB 10
varB DB 20
varC DB 30
varD DB 40
Res DB ?
.code
main1 proc
; calculate Res = (A + B) – (C + D)
mov eax,0
mov ebx,0
; calculate (A + B) – (C + D)
add eax,[varA]
add eax,[varB]
add ebx,[varC]
add ebx,[varD]
sub eax,ebx
; save the result in Res
mov [Res],eax
invoke ExitProcess,0
main1 endp
end main1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.