In: Computer Science
. Write a sequence of instructions to calculate the following arithmetic expression and store the result in register CX: 20 – 6 + (-10) - (-8) + 15
Trace the contents of registers, assume initial contents are 0000 ps(there are multiple boxes)
Instruction |
AX |
BX |
CX |
DX |
Remark |
initial |
0000 |
0000 |
0000 |
0000 |
CX:15 – 7 – 6 + (-10) + 8 - (-20)
main:
mov ecx, 15 ; eax=0 ebx=0 ecx=000F edx=0
mov ebx, 7 ; eax=0 ebx=0007 ecx=000F edx=0
sub ecx, eax,ebx ; ecx = 15-7, eax=0 ebx=0007 ecx=0008 edx=0
mov ebx, 6 ; eax=0 ebx=0006 ecx=0008 edx=0
sub ecx, ecx, ebx ; ecx = 15-7-6, eax=0 ebx=0006 ecx=0002 edx=0
mov ebx,10 ; eax=0 ebx=000A ecx=0002 edx=0
sub ebx,edx,ebx ; ebx = 0-10 = -10, eax=0 ebx=800A ecx=0002 edx=0
add ecx, ecx, ebx ; ecx = 15-7-6+(-10), eax=0 ebx=800A ecx=8008 edx=0
mov ebx, 8 ; eax=0 ebx=0008 ecx=8008 edx=0
add ecx, ecx, ebx ; ecx = 15-7-6+(-10)+8, eax=0 ebx=0008 ecx=0000 edx=0
mov ebx,20 ; eax=0 ebx=0014 ecx=0000 edx=0
sub ebx,edx,ebx ; ebx = 0-20 = -20, eax=0 ebx=8014 ecx=0000 edx=0
sub ecx, ecx, ebx ; ecx = 15-7-6+(-10)+8-(-20), eax=0 ebx=8014 ecx=0014 edx=0
ecx = 0014(hex) => 20(dec) => final result
All the values are in hexadecimal & for negative numbers first bit is used as sign bit
i hope you will understand this concept