News:

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

beginner... when to use what eax ebx ecx edx?

Started by j00n, May 18, 2010, 04:42:03 AM

Previous topic - Next topic

j00n

how do i know when to use eax ebx ecx or edx..?

    mov eax, 1024           ; eax = 1024
    mov ebx, 768             ; ebx = 768          ....a tiny snippet from (jeffwebgamer)


just a place to store a value?

so i can only store 32 bits into eax ebx ecx or edx?

what if i do...

mov eax, [ebx]      ; eax = value of ebx           <--------is this right or do i have it reversed?
mov eax, ebx         ; eax = address of ebx

---------------------------------------

mov eax, 1513  - what is AH and AL in this case?


last question for now.. how would I mov ebh or bh into eal or al ...............is this possible? what does it look like...?


jj2007

Go to this page, and look for opcodes.hlp. The section directly below ("traps") is also very important.

joemc

how do i know when to use eax ebx ecx or edx..?
they have specific purposes that people generally use them for.  most of it seems not required anymore, but it makes sense to use them for that purpose.  eax is called the accumulator,  it also contains the integer return value from a function.  It is also is the only destination for mul and div (lower 32 bits when mul two 32 bit numbers)
 
search for "Intel ABI" as well. you will find that eax,ecx,edx need not be preserved when returned from a function. So they are great candidates to use first.


    mov eax, 1024           ; eax = 1024
    mov ebx, 768             ; ebx = 768          ....a tiny snippet from (jeffwebgamer)


just a place to store a value?
It is not just a place really, it is right in the CPU. Least delay to access, and some things need to be in registers,  for example you cannot move from memory to memory without going through a register or push/pop it on the stack.

so i can only store 32 bits into eax ebx ecx or edx?
you can store 0-32 bits,  not larger than

mov eax, [ebx]      ; eax = value of ebx           <--------is this right or do i have it reversed?
if ebx currently containes a memory address this will give you the value at that memory address in eax.  If ebx contains an invalid memory address, it will cause issues.  You would have to put a address in ebx first.

mov eax, ebx         ; eax = address of ebx
this is just going to move whatever is in ebx (that is what ever 32 bit are in it). into eax.  If ebx contained a memory address it would move it into eax, it could contain any 32 bits though.


mov eax, 1513  - what is AH and AL in this case?
AL lowest 8 bits. AH next 8 bits. simple math could answer this for you.
but windows calc.exe can too...

1513 is 10111101001 in binary.
1513 is 0x5E9 in hex.

In  hex 8 bits are 2 places. so just go two places each is the way i prefer...

or count from the right 8 places in binary, and that is AL. the next 8 are AH

AL = 11101001 = 0xE9 = 233
AH = 00000101 = 0x05 = 5


or just write  a short console program to print it out for you.


last question for now.. how would I mov ebh or bh into eal or al ...............is this possible? what does it look like...?

i am not familar with ebh or eal... do you mean ebx, and eax?  if you want there values "zero-extended" you would use movzx. i would do a search for that as well.
MOVZX EAX,BH
would be similar to
MOV EAX,0
MOV AL,BH

j00n

thanks for the info... i'm kinda getting it (trying to read everything I can)

hutch--

j00n,

Something that takes a while to understand but worth the effort is that registers are determined by their SIZE and many of the errors that a learner gets are related to trying to fit the wrong size data into a register.

The contents of a 32 bit register simply will not fit into an 8 or 16 bit register but it can be done the other way, you can either ZERO EXTEND or SIGN EXTEND an 8 or 16 bit value into a 32 bit register.

If a value in a 32 bit register is within either signed or unsigned BYTE range (0 - 255) or (128+ to 128-) then you can take the 8 bit part of the register and copy it to another 8 bit register.


mov eax, 32
mov cl, al


This works because the number "32" is within the range of a BYTE so its written to the LOW BYTE of the 32 bit register EAX. AL is the LOW BYTE of EAX.

The same technique works with a 16 bit WORD sized value.


mov eax, 64
mov CX, AX


With data at known memory locations, doing the size change is easy enough to do.

If a memory location has the number in 8 bits (1 BYTE) you can do this. [esi] is the address of memory in this example.


movzx eax, BYTE PTR [esi]


EAX now holds the ZERO EXTENDED value of the BYTE at the address in the ESI register.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

j00n

i'm currently reading this... http://www.madwizard.org/programming/tutorials/ (win32asm tutorial)

nice start... then after that i'm gonna go through all the examples that came with masm....

I need to do some more reading and then come back to the forums I think...


EDIT: by the way anybody that is having trouble with .chm files on vista / windows 7 ... right click  > properties > unblock and it should work!

j00n

hey I can't go to sleep! but i'm working on a multi example app... maybe I can post it when i'm done, i'm commenting everything.

its basically like a few examples I learned starting out in vb, using controls etc

progress = molasses