News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

[Help] - Digit Sum

Started by yq8, May 05, 2015, 03:38:23 AM

Previous topic - Next topic

dedndave

another problem....

you zero EBX before you preserve it
preserve it first, then do whatever you want with it, then restore it

dedndave

it looks like you are passing only 1 DWORD argument

try this code...
        push    ebx
        push    ebp
        mov     ebp,esp
        mov     eax,[ebp+12]
        xor     ebx,ebx
        mov     ecx,10

jx:     xor     edx,edx
        idiv    ecx
        add     ebx,edx
        test    eax,eax
        jne     jx

        xchg    eax,ebx
        leave
        pop     ebx
        ret     4

yq8


yq8

One. An Integer.
Btw: I just noticed, this is the wrong thread I posted this in :D
I am trying to calculate the amount of digits of the int, not calculating the digit sum ^^ (my bad).
Tho I am somehow doing something wrong :/

dedndave

ok - digit count...
        push    ebx
        push    ebp
        mov     ebp,esp
        xor     ebx,ebx
        mov     eax,[ebp+12]
        mov     ecx,10
        jmp     jy

jx:     xor     edx,edx
        idiv    ecx
        inc     ebx

jy:     test    eax,eax
        jne     jx

        xchg    eax,ebx
        leave
        pop     ebx
        ret     4

Gunther

Dave,

that's the right approach. But why not:

    push        ebp
    mov         ebp, esp
    push        ebx

    ...

    pop         ebx
    leave
    ret          4


Gunther
You have to know the facts before you can distort them.

dedndave

Gunther - that's how the assembler does it if you let it handle the stack frame
i prefer to preserve registers before pushing and setting EBP

the reason is.....

1) preserve registers
2) set stack frame
3) push whatever locals you might like or create buffers
4) LEAVE - no need to balance the stack to get rid of locals and/or buffers
5) restore registers

i wish the assembler did it that way

dedndave

actually, because he has no locals - only has 1 argument - and only accesses it 1 time....
no need to use EBP - just grab the arg via ESP

dedndave

        push    ebx
        mov     ecx,10
        xor     ebx,ebx
        mov     eax,[esp+8]
        jmp     jy

jx:     xor     edx,edx
        idiv    ecx
        inc     ebx

jy:     test    eax,eax
        jne     jx

        xchg    eax,ebx
        pop     ebx
        ret     4

rrr314159

Quote from: dedndave on May 20, 2015, 08:51:09 AM
Gunther - that's how the assembler does it if you let it handle the stack frame
i prefer to preserve registers before pushing and setting EBP
i wish the assembler did it that way

- To each his own, I prefer the assembler's way (which I always thought standard) for one reason, that way the first variable always in the same place, ebp+8 (using stack frame) or esp+4. If u preserve reg's first it will vary depending on how many registers. Often as one hacks at the code one might preserve an extra reg, or remove one: you'd have to change offset every time. If u save, say, five regs, u have to figure out 4th parameter is at ebp+40; then change to 32 if u remove two regs from list ... assembler way, always the same. Easier for those of us who avoid thinking whenever possible :biggrin:
I am NaN ;)

dedndave

that's fine
but, my way is the right way   :lol:

you can create symbols for all the arguments and locals
then, it's not hard to make changes...

_lpNebulaRect    TEXTEQU <DWORD PTR [EBP+20]>
;RETurn address                     [EBP+16]
;EBX saved contents                 [EBP+12]
;ESI saved contents                 [EBP+8]
;EDI saved contents                 [EBP+4]
;EBP saved contents                 [EBP]
_dwTranslateX    TEXTEQU <DWORD PTR [EBP-4]>
_dwTranslateY    TEXTEQU <DWORD PTR [EBP-8]>
_dwWidth         TEXTEQU <DWORD PTR [EBP-12]>
_dwHeight        TEXTEQU <DWORD PTR [EBP-16]>
_lpPalette       TEXTEQU <DWORD PTR [EBP-20]>

Gunther

Dave,

Quote from: dedndave on May 20, 2015, 09:31:45 PM
that's fine
but, my way is the right way   :lol:

good to know, but rrr isn't wrong. I prefer his way.

Gunther
You have to know the facts before you can distort them.

dedndave

it's ok with me - lol
you guys have to balance the stack before restoring registers upon exit
i just do a LEAVE and restore them   :P
whose code is faster/more efficient ?

nidud

#28
deleted

Gunther

Dave,

Quote from: dedndave on May 21, 2015, 08:16:07 PM
it's ok with me - lol
you guys have to balance the stack before restoring registers upon exit
i just do a LEAVE and restore them   :P
whose code is faster/more efficient ?

are you sure? What about this:

    push        ebp
    mov         ebp, esp
    sub         esp, 8
    push        ebx
    push        edi
    push        esi

    ...

    pop         esi
    pop         edi
    pop         ebx
    leave
    ret


Gunther
You have to know the facts before you can distort them.