flat assembler
Message board for the users of flat assembler.

Index > Windows > Beginner Assembly Question

Author
Thread Post new topic Reply to topic
ragnar_graybeard87



Joined: 28 Mar 2016
Posts: 4
ragnar_graybeard87 28 Mar 2016, 01:13
Hey guys,

I included the source for the program in question. Basically, I'm following the UDEMY asm tutorial by XorPD and I'm stuck on one of the questions. It says to get a 32-bit integer as input then decompose it into 4 bytes then add them. Obviously the AL and AH were easy to get at but it says to figure out the extended 4 bytes by using DiV.

My approach was to save AL and AH to another reg then zero out AX then keep dividing EAX by ECX (inc'ing by 1 till they matched) in hopes that it'd save the bytes into CX but it seems to place them into the extended portionl. I guess all I'm asking is how could I access those top bytes without using a fancy instruction which we haven't been introduced to yet? I'm sure its simpler than I'm making it out to be however I can't figure it out on my own so any suggestions and pointers would be highly appreciated!

Thanks in advance Smile


Description:
Download
Filename: 0_question.asm
Filesize: 2.09 KB
Downloaded: 396 Time(s)

Post 28 Mar 2016, 01:13
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1635
Location: Toronto, Canada
AsmGuru62 28 Mar 2016, 01:50
Isn't it better to store 32-bit value into memory and then add the separate bytes from that memory location?
Code:
        ;
        ; EAX is the input
        ;
        push    eax             ; store it on stack
        mov     esi, esp        ; point ESI to the value (4 bytes of it) you just stored
        ;
        ; Add the bytes at ESI
        ;
        xor     eax, eax        ; EAX = 0
        xor     ecx, ecx        ; ECX = 0
        ;
        ;  Do it for all 4 bytes
        ;
        mov     cl, [esi]
        add     eax, ecx

        mov     cl, [esi + 1]
        add     eax, ecx

        mov     cl, [esi + 2]
        add     eax, ecx

        mov     cl, [esi + 3]
        add     eax, ecx
        ;
        ; EAX now has the sum of 4 bytes from initial input
        ;
        pop     ecx             ; restore stack pointer
    

Or you can also do it by shifting the 32-bit value by 8 bits:
Code:
        ;
        ; EAX is the input value
        ;
        xor     ecx, ecx        ; ECX = 0
        mov     cl, al          ; 1st byte into ECX
        xor     edx, edx        ; EDX = 0

        shr     eax, 8
        mov     dl, al
        add     ecx, edx        ; ECX is a sum of 2 bytes

        shr     eax, 8
        mov     dl, al
        add     ecx, edx        ; ECX is a sum of 3 bytes

        shr     eax, 8
        mov     dl, al
        add     ecx, edx        ; ECX is a final sum
    

EDITED:
Oh, I see, you need to use DIV.
If you divide EAX just once (no need for a loop) by 10000h - you will get the higher 16 bits into AX, so then use AH,AL to add them.
Code:
        ;
        ; EAX = input
        ;
        xor     ecx, ecx        ; ECX = 0
        xor     edx, edx        ; EDX = 0

        mov     cl, al
        mov     dl, ah
        add     ecx, edx        ; ECX = sum of AH + AL
        ;
        ; Divide EAX by 10000h
        ;
        mov     esi, 10000h
        xor     edx, edx
        div     esi
        ;
        ; Repeat the trick with AH + AL
        ;
        xor     edx, edx

        mov     dl, ah
        add     ecx, edx

        mov     dl, al
        add     ecx, edx
        ;
        ; ECX is the final sum
        ;
    
Post 28 Mar 2016, 01:50
View user's profile Send private message Send e-mail Reply with quote
ragnar_graybeard87



Joined: 28 Mar 2016
Posts: 4
ragnar_graybeard87 28 Mar 2016, 02:41
That's amazing, I've been pulling my hair out trying to figure it out. I wouldn't have guessed the 10000h in a million years. In fact I realize I wasn't even doing the addition properly. I thought I'd be able to do this once I got the bytes in the right containers:

add ah,ch
add al,cl

then the sum of all 4 would be inside AH and AL which should be AX so then I'd:

movzx EAX,AX
call print_eax

but I never got the desired result. Thank you again for helping me with this its very very appreciated. Now I can figure out why this code does what it does and start learning bitwise operations Smile
Post 28 Mar 2016, 02:41
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.