Lecture 2
Lecture 2
X86 REGISTERS,MEMORY
ADDRISING& INSTRUCTIONS IN
ASSEMBLY LANGUAGE
By
Dr. Mohammed Y.M Alnaham
• x86 processors have eight 32-bit general purpose registers, as depicted in Figure
1.
• The register names are mostly historical. For example, EAX used to be called the
accumulator since it was used by a number of arithmetic operations.
• ECX was known as the counter since it was used to hold a loop index.
• Whereas most of the registers have lost their special purposes in the modern
instruction set, by convention, two are reserved for special purposes — the stack
pointer
2
(ESP) and the base pointer (EBP).
Dr. Mohammed Y.M Alnaham
x86 Registers 16 bits
8 bits 8 bits
General-purpose Registers
EAX AX AH AL
EBX BX BH BL
ECX CX CH CL
EDX DX DH DL
ESI
EDI
ESP
(stack pointer)
EBP
(base pointer) 32 bits
IP
(instruction pointer)
• For the EAX, EBX, ECX, and EDX registers, subsections may be used.
• When referring to registers in assembly language, the names are not case-
sensitive. For example, the names EAX and eax refer to the same register.
.DATA
var DB 64 ; Declare a byte, referred to as location var, containing the value 64.
DB 10 ; Declare a byte with no label, containing the value 10. Its location is var2 + 1.
Addressing Memory
Addressing Memory
mov eax, [ebx] ; Move the 4 bytes in memory at the address contained
in EBX into EAX
mov [var], ebx ; Move the contents of EBX into the 4 bytes at memory address var.
(Note, var is a 32-bit constant).
mov eax, [esi-4] ; Move 4 bytes at memory address ESI + (-4) into EAX
mov [esi+eax], cl ; Move the contents of CL into the byte at address ESI+EAX
Dr. Mohammed Y.M Alnaham 12
Addressing Memory : Examples
• mov edx, [esi+4*ebx] ; Move the 4 bytes of data at address ESI+4*EBX into EDX
• The size of the memory regions could be inferred from the size of the
register operand.
• When we were loading a 32-bit register, the assembler could infer that
the region of memory we were referring to was 4 bytes wide.
• When we were storing the value of a one byte register to memory, the
assembler could infer that we wanted the address to refer to a single
Dr. Mohammed Y.M Alnaham 14
x86 Assembly:- Memory and Addressing Modes
Declaring Static Data Regions: Size Directives
• Consider the instruction mov [ebx], 2. Should this instruction move the value 2
into the single byte at address EBX? Perhaps it should move the 32-bit integer
representation of 2 into the 4-bytes starting at address EBX.
mov WORD PTR [ebx], 2 ; Move the 16-bit integer representation of 2 into the 2 bytes starting at the address
in EBX.
mov DWORD PTR [ebx], 2 ; Move the 32-bit integer representation of 2 into the 4 bytes starting at the
address in EBX.
• mov — Move (Opcodes: 88, 89, 8A, 8B, 8C, 8E, ...)
• The mov instruction copies the data item referred to by its second
operand (i.e. register contents, memory contents, or a constant value)
into the location referred to by its first operand (i.e. a register or
memory).
• While register-to-register moves are possible, direct memory-to-
memory moves are not.
• In cases where memory transfers are desired, the source memory
contents must first be loaded into a register, then can be stored to the
destination memory address
19 Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsData Movement Instructions 86
• mov <reg>,<const>
• mov <mem>,<const>
20 Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsData Movement Instructions 86
• push — Push stack (Opcodes: FF, 89, 8A, 8B, 8C, 8E, ...)
• The push instruction places its operand onto the top of the
hardware supported stack in memory.
• Specifically, push first decrements ESP by 4, then places its operand
into the contents of the 32-bit location at address [ESP].
• ESP (the stack pointer) is decremented by push since the x86 stack
grows down - i.e. the stack grows from high addresses to lower
• addresses.
Examples
• Syntax of push push eax ; copy eax to the top element
of the stack
• push <reg32>
push [var] ;push the 4 bytes at address
• push <mem> var onto the stack
• push <con32>
• The pop instruction removes the 4-byte data element from the
top of the hardware-supported stack into the specified operand
(i.e. register or memory location).
• It first moves the 4 bytes located at memory location [SP] into
the specified register or memory location, and then increments
23 SP by 4. Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsData Movement Instructions 86
Examples
• Syntax of pop pop edi ;pop the top element of the stack
.into EDI
• pop <reg32>
pop [ebx] ; pop the top element of the
• pop <mem> stack into memory at the four bytes
.starting at location EBX
Examples
• Syntax of lea lea eax, [var] ; the address of var is placed
.in EAX
• lea <reg32>,<mem>
lea edi, [ebx+4*esi]; the quantity
EBX+4*ESI is placed in EDI.
• Stack
• Data
• Code
The size of code and data of a program can be specified by memory model
using .MODEL directive
.MODEL Memory_model
• The stack area should be big enough to contain the stack at its
maximum size.
• Declaration:
.STACK size
.STACK 100H
** Allocates 256 bytes for stack area reasonable size for most
applications
.DATA
WORD1 DW 2
BYTE1 DB 1
Declaration:
name PROC
name ENDP
Here name = name of the procedure. PROC and ENDP are pseudo-ops
Program Structure
.MODEL SMALL
.STACK 100H
.DATA
. CODE
MAIN PROC
;instructions go here
MAIN ENDP
END MAIN
*** The last line of the program should be the END directive, followed by the name of main
procedure
.MODEL SMALL ; Define the memory model
.STACK 100h ; Define the stack size
.DATA ; Data segment
helloMessage DB 'Hello, World!$', 0 ; String to display
1. Definitions: Define each segment register (CS, DS, SS, ES, FS, GS) and explain its
purpose in the x86 architecture.
2. Segmentation: Describe how segmentation allows for memory management
and the advantages it provides over flat memory models
3. Address Calculation: Explain how the effective address is calculated using
segment registers and offsets. Provide an example calculation.