Assembly basic's #3 - REGISTERS

Posts 18 of 8 · Page 1 of 1
Assembly basic's #3 - REGISTERS
THE REGISTERS:


I've mentioned AX, AL, and AH before, and you're probably wondering what
exactly they are. Well, I'm gonna go through one by one and explain
what each register is and what it's most common uses are. Here goes:


AX (AH/AL):
AX is a 16 bit register which, as metioned before, is merely two bytes
attached together. Well, for AX, BX, CX, & DX you can independantly
access each part of the 16 bit register through the 8bit (or byte sized)
registers. For AX, they are AL and AH, which are the Low and High parts
of AX, respectivly. It should be noted that any change to AL or AH,
will change AX. Similairly any changes to AX may or may not change AL and
AH. For instance:


Let's suppose that AX = 00000h (AH and AL both = 0, too)

mov AX,0
mov AL,0
mov AH,0

Now we set AL = 0FFh.

mov AL,0FFh

:AX => 000FFh ;I'm just showing ya what's in the registers
:AL => 0FFh
:AH => 000h

Now we increase AX by one:

INC AX

:AX => 00100h (= 256.. 255+1= 256)
:AL => 000h (Notice that the change to AX changed AL and AH)
:AH => 001h

Now we set AH = 0ABh (=171)

mov AH,0ABh

:AX => 0AB00h
:AL => 000h
:AH => 0ABh

Notice that the first example was just redundant...
We could've set AX = 0 by just doing

mov ax,0

:AX => 00000h
:AL => 000h
:AH => 000h

I think ya got the idea...


SPECIAL USES OF AX:
Used as the destination of an IN (in port)
ex: IN AL,DX
IN AX,DX

Source for the output for an OUT
ex: OUT DX,AL
OUT DX,AX

Destination for LODS (grabs byte/word from [DS:SI] and INCreses SI)
ex: lodsb (same as: mov al,[ds:si] ; inc si )
lodsw (same as: mov ax,[ds:si] ; inc si ; inc si )

Source for STOS (puts AX/AL into [ESI] and INCreses DI)
ex: stosb (same as: mov [es:di],al ; inc di )
stosw (same as: mov [es:di],ax ; inc di ; inc di )

Used for MUL, IMUL, DIV, IDIV

BX (BH/BL): same as AX (BH/BL)

SPECIAL USES:
As mentioned before, BX can be used as an OFFSET register.
ex: mov ax,[ds:bx] (grabs the WORD at the address created by
DS and BX)

CX (CH/CL): Same as AX

SPECIAL USES:
Used in REP prefix to repeat an instruction CX number of times
ex: mov cx,10
mov ax,0
rep stosb ;this would write 10 zeros to [ESI] and increase
;DI by 10.
Used in LOOP
ex: mov cx,100
THELABEL:

;do something that would print out 'HI'

loop THELABEL ;this would print out 'HI' 100 times
;the loop is the same as: dec cx
jne THELABAL

DX (DH/DL): Same as above
SPECIAL USES:
USED in word sized MUL, DIV, IMUL, IDIV as DEST for high word
or remainder

ex: mov bx,10
mov ax,5
mul bx ;this multiplies BX by AX and puts the result
;in DX:AX

ex: (continue from above)
div bx ;this divides DX:AX by BX and put the result in AX and
;the remainder (in this case zero) in DX

Used as address holder for IN's, and OUT's (see ax's examples)

INDEX REGISTERS:

DI: Used as destination address holder for stos, movs (see ax's examples)
Also can be used as an OFFSET register

SI: Used as source address holder for lods, movs (see ax's examples)
Also can be used as OFFSET register

Example of MOVS:
movsb ;moves whats at [DS:SI] into [ESI] and increases
movsw ; DI and SI by one for movsb and 2 for movsw

NOTE: Up to here we have assumed that the DIRECTION flag was cleared.
If the direction flag was set, the DI & SI would be DECREASED
instead of INCREASED.
ex: cld ;clears direction flag
std ;sets direction flag

STACK RELATED INDEX REGISTERS:
BP: Base Pointer. Can be used to access the stack. Default segment is
SS. Can be used to access data in other segments throught the use
of a SEGMENT OVERRIDE.

ex: mov al,[ES:BP] ;moves a byte from segment ES, offset BP
Segment overrides are used to specify WHICH of the 4 (or 6 on the
386) segment registers to use.

SP: Stack Pointer. Does just that. Segment overrides don't work on this
guy. Points to the current position in the stack. Don't alter unless
you REALLY know what you are doing.

SEGMENT REGISTERS:
DS: Data segment- all data read are from the segment pointed to be this
segment register unless a segment overide is used.
Used as source segment for movs, lods
This segment also can be thought of as the "Default Segment" because
if no segment override is present, DS is assumed to be the segmnet
you want to grab the data from.

ES: Extra Segment- this segment is used as the destination segment
for movs, stos
Can be used as just another segment... You need to specify [ES:°°]
to use this segment.

FS: (386+) No particular reason for it's name... I mean, we have CS, DS,
and ES, why not make the next one FS? Just another segment..

GS: (386+) Same as FS


OTHERS THAT YOU SHOULDN'T OR CAN'T CHANGE:
CS: Segment that points to the next instruction- can't change directly
IP: Offset pointer to the next instruction- can't even access
The only was to change CS or IP would be through a JMP, CALL, or RET

SS: Stack segment- don't mess with it unless you know what you're
doing. Changing this will probably crash the computer. This is the
segment that the STACK resides in.

Heck, as long as I've mentioned it, lets look at the STACK:

The STACK is an area of memory that has the properties of a STACK of
plates- the last one you put on is the first one take off. The only
difference is that the stack of plates is on the roof. (Ok, so that
can't really happen... unless gravity was shut down...) Meaning that
as you put another plate (or piece of data) on the stack, the STACK grows
DOWNWARD. Meaning that the stack pointer is DECREASED after each PUSH,
and INCREASED after each POP.

_____ Top of the allocated memory in the stack segment (SS)
þ
þ
þ
þ ® SP (the stack pointer points to the most recently pushed byte)

Truthfully, you don't need to know much more than a stack is Last In,
First Out (LIFO).

WRONG ex: push cx ;this swaps the contents of CX and AX
push ax ;of course, if you wanted to do this quicker, you'd
...
pop cx ;just say XCHG cx,ax
pop ax ; but thats not my point.

RIGHT ex: push cx ;this correctly restores AX & CX
push ax
...
pop ax
pop cx
jetamay or some on rename this registers & stacks
What does the X stand for?

(such as EAX = Enhanced(32bit) Accumulator(counter) X_______)
AltF5 that is a good question but in 'Accumulator(counter)' the (counter) isn't really valid. You'll find the Accumulator register is most used in calculations and receiving the last bits of input/prompted information, and EC(count)X handles counts, increments, decrements and loop times most commonly. I've never put any thought into the X and just assumed it has some clever title, which inevitably means the same thing, its a 'variable' of some assemblic sort. I also haven't found any thing quick on google so, feel free to enlighten us if there's more to it. The X may just mean register.
Yea, I never knew either (Although I am very new to assembly)

My only guess was that it means either indeX or ConteXt (as in context switches)

Possibly the "x" could mean the same thing as whatever it means in x86 and x64.

The meaning of x86 and x64. - Andre Da Costa - Extended64 - The 64-Bit Windows Community
Here, it says the X represents the general family of xxx86. (whatever the hell that means)

Or perhaps the creator just felt that X will always just stand for a register.



Also, thank for explaining the deal with that not being a counter.
In truth I guess it doesn't really matter what register is used for a counter or data since they are all variables right?
You can use any you want when reversing, so long as you restore balance after wards, indeed BUT, the default registers your CPU always tends to use, will remain the same and thus it is some what important. Good post AltF5 and thanks for the link. Oh and, you did good too with the tutorial Putin. Jetamay brought up a good point to me: You have AH and AL so many AX means both =p X representing 'Full.'
many of the early naming conventions have lost their meaning. I started assembly programing on win98, however most of the tutorials i could find at the time involved virus creation on dos systems (16bit).

As far as I can tell (thinking about how they would name registers) they would have just given them names such as the multi-use registers :register A register B register C register D (ax bx cx dx respectively cx was the counter as it was easier to remember and ax bx were used for input output from "in" and "out" commands) in which the x just showed that it was a register (to keep with the two character naming convention and since "X" is a normal placeholder) then you had the souce index and destination index (si and di). There was one required to point at the data segment and the code segment and the stack segment and the extra segement (ds and cs and ss and es, later x86 added the fs and gs multiuse segment registers however they can play special roles and shouldn't be used as multiuse registers themselves) we needed a stack pointer and the base (stack) pointer (sp and bp, the latter used to keep track of where the stack was on entry to the procedure).

However, as processors got better and more standardized (32bit) the registers had to be "E"xpanded or "E"nhanced. Most of the segment registers are all forced to 0 now due to paging and are defuncted, but are still around (again, i wouldn't touch them unless I knew what I was doing.) But inorder to keep everyone from thinking they are learning a completely new language, they make a couple minor changes while meanings are lost.
So ur saying they chose "x" cuz it usually stands for a variable in math? And Registers are more like variables then Pointers or Indexes.... and since technically Pointers and Indexes are registers too it would be redundant to name them registers with an "R"


ANYWAY.... I have a question:
I dont understand the fkin structure of Assembly. Seems like shit is thrown everywhere. I can tell which lines interact with each other. In higher languages stuff thats together is enclosed in brackets or something. in Assembly its not.... So how can u tell what is in the loop and wats not? Whay is affected by the comparison and wats not ?
Posts 18 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Need help?