Assign values to registers and add and subtract them

时光怂恿深爱的人放手 提交于 2019-12-02 15:33:02

问题


I am completely lost with this. I need to program that calculates the following expression, using registers: varA = (varA + varB) − (varC + varD), where varA, varB, etc., are variables. Assign integer values to the EAX, EBX, ECX, and EDX registers for the aforementioned variables. (Meaning, you may hardcode the inputs)

My Code:

; AddTwo.asm - adds two 32-bit integers.
; Chapter 3 example

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data

varA  dword 5
varB  dword 3
varC  dword 4
varD  dword 1

.code
main proc
mov eax,varA
add    eax,varB

 mov    ecx,varC
 add    ecx,varD

 mov  ebx,varA
 sub  edx,varD

invoke ExitProcess,0
main endp
end main

回答1:


mov    eax,varA
add    eax,varB
mov    ecx,varC
add    ecx,varD

This all makes perfect sense, eax = varA + varB. ecx = varC + varD.

At this point, just

sub eax, ecx

to get eax = (varA + varB) - (varC + varD).

I'm not sure what was intended with

mov  ebx,varA
sub  edx,varD

That sets ebx = varA, and then sets edx = edx - varD. edx wasn't defined before, so this is a random number. Or maybe edx was originally cleared to zero, in which case edx is now equal to -varD. I wouldn't count on it though.



来源:https://stackoverflow.com/questions/52595225/assign-values-to-registers-and-add-and-subtract-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!