flat assembler
Message board for the users of flat assembler.

 Index > Windows > Beginner Assembly Question
Author
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!

28 Mar 2016, 01:13
AsmGuru62

Joined: 28 Jan 2004
Posts: 1496
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]

mov     cl, [esi + 1]

mov     cl, [esi + 2]

mov     cl, [esi + 3]
;
; 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

mov     dl, al
;
; ECX is the final sum
;
```
28 Mar 2016, 01:50
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:

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
28 Mar 2016, 02:41
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum