DEV Community

UPinar
UPinar

Posted on

Daily Dose of x86-64 - I

x86-64 registers

These are registers in x86-64 assembly language. We will use some of those registers and do some experiments with them.

assembly code

In the picture above, we assign AX register a value of 259. AX is a 16 bit register. When we convert decimal value of 259 to binary we will get 0001 0000 0011.

259d = 0001 0000 0011b
Enter fullscreen mode Exit fullscreen mode

Because of AX is 16 bit register it can store decimal 259. But what about AL register inside AX register which is 8 bit register, Can AL store 259d?

RAX register

AL register (8 bit) is lower half of the AX register. When we are storing values inside AX register we also use AH and AL registers. In our situation AH and AL stores,

AH  0000 0001 AL 0000 0011
AH = (2^8 * 1) = 256 
AL = (2^1 * 1) + (2^0 * 1) = 3 
TOTAL 256 + 3 = 259d
Enter fullscreen mode Exit fullscreen mode

So what actually happens to AH and AL registers when we try to store 259 in AX register?

8 bit is for
unsigned types 0 to 255
signed types -128 to 127

unsigned type 0 to (2^8 - 1)
signed type (-2^8 / 2) to (2^8 / 2 - 1)
Enter fullscreen mode Exit fullscreen mode

Answer to AL is, because of it is an 8 bit register and the value that will stored in AX is more than max value that AL (first part of AX) can store, it will deny the value bigger than 8 bit memory. What that mean is, it will take the modulus of the value by cutting out all the pieces of full 8 bit memories (1111 1111b) from it and use the value that lefts.

259d % 256 = 3 
//because of the max value is 255 we need to
//take modulus of 256 for able to get 255 also in modulus result.
AL 0000 0011
Enter fullscreen mode Exit fullscreen mode

Answer to AH is, It will store the value that AL can not stored and because of AX = AH + AL. 1st bit of AH is actually 9th bit of AX.
For AX, AH will store 256 but for AH it is storing 1.

AX 0000 0001 0000 0011
AH 0000 0001
AL 0000 0011
Enter fullscreen mode Exit fullscreen mode

values stored in registers

These are the registers and variables and values that they store as we saw in the second picture. 

mov ax, 259
We can see WORD size (16 bit) AX register stores 259d. 
Upper half of AX which is BYTE size AH (8 bit) stores 1d, 0000 0001b.
Lower half of AX which is also BYTE size AL (8 Bit) stores 3d 0000 0011b. 

mov bl, 257
BL (8 bit) register stores 1d = 0000 0001b (257 % 256 = 1).

mov BYTE[number], 256 
BYTE size number variable stores 0d = 0000 0000b (256 % 256 = 0).

Top comments (0)