The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Wez on January 07, 2008, 12:28:12 AM

Title: Strange numbers with print?
Post by: Wez on January 07, 2008, 12:28:12 AM
Hi, I'm new MASM, therefore I have a few questions...

I'm trying to keep track of some values as they go through a series of calculations, so I kept 'print' ing out the value as they went through each step but kept coming out with some very strange numbers so I experimented with all the GP registers and confirmed my suspicions. What I'm asking is, is there a fix for this, which include file has the print and str$ macros? or am I just doing it all wrong and need to use some other instruction?

The experiment code is as follows:

mov eax, 16
mov ebx, 32
mov ecx, 64
mov edx, 128
mov esi, 256
mov edi, 512

print str$(eax)
print str$(ebx)
print str$(ecx)
print str$(edx)
print str$(esi)
print str$(edi)

print chr$(13, 10)

print str$(eax)
print str$(ebx)
print str$(ecx)
print str$(edx)
print str$(esi)
print str$(edi)

exit

What I expected was:

16
32
64
128
256
512

16
32
64
128
256
512

But instead I got:

16
32
2011667995
1
256
512

2
32
2011667995
1
256
512

Leaving the only general purpose registers that can be used for print and calculations are, ebx, esi, and edi.
Is this right?

Thank you in advance for any help
Wez
Title: Re: Strange numbers with print?
Post by: raymond on January 07, 2008, 04:29:58 AM
QuoteLeaving the only general purpose registers that can be used for print and calculations are, ebx, esi, and edi.
Is this right?

Absolutely right. Windows API and MASM32 functions are expected to (and usually do) preserve the ebx, esi and edi registers. However, the EAX, ECX and EDX registers are considered free to be trashed. Whenever you need to preserve such registers, you need to save them before you call external functions and restore them when you need them. For example,

push edx
push ecx
print str$(eax)
print str$(ebx)
pop  ecx
print str$(ecx)
pop  edx
print str$(edx)

Raymond
Title: Re: Strange numbers with print?
Post by: MichaelW on January 07, 2008, 04:41:49 AM
Wez,

For more information see \masm32\help\asmintro.hlp: Register Preservation Convention.
Title: Re: Strange numbers with print?
Post by: Wez on January 07, 2008, 07:34:46 AM
Thanks guys, isn't it easy when you know how.

Wez :bg