The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Ani_Skywalker on July 10, 2011, 09:34:02 PM

Title: aritmetics :(
Post by: Ani_Skywalker on July 10, 2011, 09:34:02 PM
My code does not generate what I expect it to, code follows below


   include \masm32\include\masm32rt.inc

.code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc
    LOCAL holder    :DWORD

    xor eax, eax
    xor edx, edx
    mov eax, 5
    mov edx, 3

    print ustr$(eax),13,10,13,10
    print ustr$(edx),13,10,13,10
    print ustr$(eax),13,10,13,10
    print ustr$(edx),13,10,13,10
    print ustr$(eax),13,10,13,10
    add eax, edx
    print ustr$(eax),13,10,13,10
    inkey
    ret

main endp

exit

end start


And this is the output:

5

582600

4

582600

4

582604




But I expect the following output

5

3

5

3

5

8


What am I doing wrong?
Title: Re: aritmetics :(
Post by: oex on July 10, 2011, 09:38:55 PM
print ustr$(eax),13,10,13,10

The value in eax (and edx) is likely changed by the ustr$ macro.... And also by the print macro....

push eax
push edx
    print ustr$(eax),13,10,13,10
pop edx
pop eax
push eax
push edx
    print ustr$(edx),13,10,13,10
pop edx
pop eax

etc etc

This will work.... Alternatively use the ebx, esi, edi registers as these are preserved by the macros

However if you do this also:

main proc USES ebx esi edi
Title: Re: aritmetics :(
Post by: Ani_Skywalker on July 10, 2011, 09:47:02 PM
Thank you, that explained it all.

Where do I find the source-code for the MASM32-macros? I'm using version 10 and I searched the files in the macros folder but I couldn't seem to find the macros I searched for. For example the rv-macro (return value).
Title: Re: aritmetics :(
Post by: oex on July 10, 2011, 09:56:59 PM
\masm32\macros\macros.asm

Search for *macroname* *space* 'macro'
ie

'rv macro'

(In most if not all cases you should find what you are looking for however remember also other whitespace characters may exist ie tab)
Title: Re: aritmetics :(
Post by: dedndave on July 10, 2011, 09:57:17 PM
the source code is in \masm32\macros\macros.asm
there is also a help file \masm32\help\hlhelp.chm
Title: Re: aritmetics :(
Post by: hutch-- on July 11, 2011, 04:06:47 AM
Ani,

The thing you need to learn is the register preservation convention, commonly known as the Intel ABI (Application Binary Interface), in the ordinary sense, you can modify EAX ECX EDX but must ensure that EBX ESI EDI remain the same. If you are layout out a procedure manually you must also preserve EBP and ESP. The function behind the macros all use this convention and it is important to get it right.

Now if you swapped the EAX and EDX registers in your example to ESI and EDI, the functions that are called must preserve them so you don't have to between function calls.
Title: Re: aritmetics :(
Post by: vanjast on July 11, 2011, 08:15:52 PM
As a side note..
Quote
    xor eax, eax
    xor edx, edx
    mov eax, 5
    mov edx, 3

You can do away with the XORs, as the registers are overwritten with the MOVs
:wink