News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

aritmetics :(

Started by Ani_Skywalker, July 10, 2011, 09:34:02 PM

Previous topic - Next topic

Ani_Skywalker

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?

oex

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
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Ani_Skywalker

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).

oex

\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)
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

the source code is in \masm32\macros\macros.asm
there is also a help file \masm32\help\hlhelp.chm

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

vanjast

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