assemblymasmirvine32

Writeint not displaying the value in EAX register


I have written the following assembly code.
It's displaying the output of writeint as +0
But When I put the debug point on the line before writeint and look at the registers
I see EAX=0000BFBE. As per my understanding writeint should print the value of EAX
register. What could be the reason that it's showing +0?

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

INCLUDE Irvine32.inc

.data
var1    byte    24
var2    word    4000h
var3    dword   0FFFF0000h
Z1    dword    ?
Z2    dword    ?
Z3    dword    ?
Z4    dword    ?

.code
main proc
xor eax, eax
mov al, var1
add al, 10

xor ebx, ebx
mov bx, var2
add bx, 100

sub ax,bx
mov eax, Z1
call writeint

invoke ExitProcess,0
main endp
end main

Solution

  • By putting the breakpoint on the line before, I assume you mean that you put the breakpoint here:

    mov eax, Z1
    

    Well, it seems that this instruction didn't actually execute before pausing at the breakpoint. Here's the chain of events that lead to eax being 0000BFBE:

    xor eax, eax
    mov al, var1
    add al, 10
    

    mov al, var1 sets eax to 18h and 18h + 10 is 22h, so the whole of eax is currently 00000022h.

    xor ebx, ebx
    mov bx, var2
    add bx, 100
    

    mov bx, var2 sets bx to 4000h and 4000h + 100 is 4064h.

    sub ax,bx
    

    This calculates 22h - 4064h, whose result is 0bfbeh in two's complement and thus the whole of eax is 0000bfbeh. This is the result you are seeing.

    However, mov eax, Z1 makes all that pretty much useless. Z1 has been zero-initialized, so that line sets eax to 0 as well. Which is why writeint shows 0.