0% found this document useful (0 votes)
12 views

Lecture 2

microprocessor and assembly language course lecture 2

Uploaded by

malnaham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 2

microprocessor and assembly language course lecture 2

Uploaded by

malnaham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture2

X86 REGISTERS,MEMORY
ADDRISING& INSTRUCTIONS IN
ASSEMBLY LANGUAGE
By
Dr. Mohammed Y.M Alnaham

Dr. Mohammed Y.M Alnaham 1


x86 Assembly:- Registers

• 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)

Dr. Mohammed Y.M Alnaham 3


Dr. Mohammed Y.M Alnaham 4
x86 Assembly:- Registers

• For the EAX, EBX, ECX, and EDX registers, subsections may be used.

• For example, the least significant 2 bytes of EAX can be treated as a


16-bit register called AX.

• The least significant byte of AX can be used as a single 8-bit register


called AL, while the most significant byte of AX can be used as a
single 8-bit register called AH.
These names refer to the same physical register.
Dr. Mohammed Y.M Alnaham 5
x86 Assembly:- Registers
• When a two-byte quantity is placed into DX, the update affects the value
of DH, DL, and EDX.

• These sub-registers are mainly hold-overs from older, 16-bit versions of


the instruction set. However, they are sometimes convenient when dealing
with data that are smaller than 32-bits (e.g. 1-byte ASCII characters).

• 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.

Dr. Mohammed Y.M Alnaham 6


x86 Assembly:- Memory and Addressing Modes

Declaring Static Data Regions

• In x86 assembly using special assembler directives for this


purpose.

• Data declarations should be preceded by the .DATA directive.

• Following this directive, the directives DB, DW, and DD can be


used to declare one, two, and four byte data locations,
respectively Dr. Mohammed Y.M Alnaham 7
x86 Assembly:- Memory and Addressing Modes

Declaring Static Data Regions

• Declared locations can be labeled with names for later


reference this is similar to declaring variables by name, but
abides by some lower level rules.

• For example, locations declared in sequence will be located in


memory next to one another.

Dr. Mohammed Y.M Alnaham 8


x86 Assembly:- Memory and Addressing Modes

Declaring Static Data Regions


• Example declarations:

.DATA

var DB 64 ; Declare a byte, referred to as location var, containing the value 64.

var2 DB ? ; Declare an uninitialized byte, referred to as location var2.

DB 10 ; Declare a byte with no label, containing the value 10. Its location is var2 + 1.

X DW ? ; Declare a 2-byte uninitialized value, referred to as location X.

Y DD 30000 ; Declare a 4-byte value, referred to as location Y, initialized to 30000.

Dr. Mohammed Y.M Alnaham 9


x86 Assembly:- Memory and Addressing Modes

Addressing Memory

• Modern x86-compatible processors are capable of addressing up


to 2^32 bytes (4 GB)of memory: memory addresses are 32-bits
wide.;

• In the examples above, where we used labels to refer to memory


regions, these labels are actually replaced by the assembler with
32-bit quantities that specify addresses in memory.
Dr. Mohammed Y.M Alnaham 10
x86 Assembly:- Memory and Addressing Modes

Addressing Memory

• We illustrate some examples using the mov instruction that


moves data between registers and memory.

• This instruction has two operands: the first is the destination


and the second specifies the source.

Dr. Mohammed Y.M Alnaham 11


Addressing Memory : Examples

Some examples of mov instructions using address computations are:

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

• Some examples of invalid address calculations include:

mov eax, [ebx-ecx] ; Can only add register values

mov [eax+esi+edi], ebx ; At most 2 registers in address computation

Dr. Mohammed Y.M Alnaham 13


x86 Assembly:- Memory and Addressing Modes
Declaring Static Data Regions: Size Directives

• 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

• However, in some cases the size of a referred-to memory region is ambiguous.

• 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.

• Since either is a valid possible interpretation, the assembler must be explicitly


directed as to which is correct. The size directives BYTE PTR, WORD PTR, and
DWORD PTR serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively .
Dr. Mohammed Y.M Alnaham 15
x86 Assembly:- Memory and Addressing Modes
Declaring Static Data Regions: Size Directives : example:
mov BYTE PTR [ebx], 2 ; Move 2 into the single byte at the address stored
in 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.

Dr. Mohammed Y.M Alnaham 16


x86 Assembly:- Instructions
Machine instructions
categories

data movement arithmetic/logic control-flow.

17 Dr. Mohammed Y.M Alnaham


x86 Assembly:- Instructions
• We will look at important examples of x86 instructions from each category.
• We use the following notation:
1) <reg32> Any 32-bit register (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP)
2) <reg16> Any 16-bit register (AX, BX, CX, or DX)
3) <reg8> Any 8-bit register (AH, BH, CH, DH, AL, BL, CL, or DL)
4) <reg> Any register
5) <mem> A memory address (e.g., [eax], [var + 4], or dword ptr [eax+ebx])
6) <con32> Any 32-bit constant
7) <con16> Any 16-bit constant
8) <con8> Any 8-bit constant
9) <con> Any 8-, 16-, or 32-bit constant
18 Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsData Movement Instructions 86

• 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:- InstructionsData Movement Instructions 86

• Syntax of mov Examples


mov eax, ebx ; copy the value in ebx
• mov <reg>,<reg> . into eax
• mov <reg>,<mem> mov byte ptr [var], 5 ; store the value 5
into the byte at location var
• mov <mem>,<reg>

• mov <reg>,<const>

• mov <mem>,<const>
20 Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsData 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.

21 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsData Movement Instructions 86

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>

22 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsData Movement Instructions 86

• pop — Pop stack

• 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:- InstructionsData 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

24 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsData Movement Instructions 86

• lea — Load effective address


• The lea instruction places the address specified by its second
operand into the register specified by its first operand.
Note, the contents of the memory location are not loaded, only
the effective address is computed and placed into the register.
• This is useful for obtaining a pointer into a memory region.

25 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsData Movement Instructions 86

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.

26 Dr. Mohammed Y.M Alnaham


Program Structure
 A program Consist of

• Stack

• Data

• Code

 Each part occupies memory segments

 Program segment is translated into memory segment by assembler.

 The size of code and data of a program can be specified by memory model
using .MODEL directive

.MODEL Memory_model

.MODEL SMALL [Code in ONE segment and Data in one segment]


Stack Segment
• Allocate a block of memory (stack area) to store the stack.

• 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

** If size is omitted, 1KB is allocated for stack area automatically.


Data Segment
 Contains all the variable definitions and sometimes Constant
definitions (constant does not take any memory).

 To declare data segment .DATA directive is used followed by


variable and constant declaration.

.DATA

WORD1 DW 2

BYTE1 DB 1

MSG DW ‘THIS IS A MESSAGE’

MASK EQU 10010001B


Code Segment
 Contains the program’s instructions

 Declaration:

 .CODE name [name is optional]

There is no need of name in SMALL program

 Inside a code segment, instructions are organized as procedures.

name PROC

; body of the procedure

name ENDP

 Here name = name of the procedure. PROC and ENDP are pseudo-ops
Program Structure
.MODEL SMALL

.STACK 100H

.DATA

; data definitions here

. CODE

MAIN PROC

;instructions go here

MAIN ENDP

;other procedures go here

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

.CODE ; Code segment


main PROC
; Initialize the data segment
MOV AX, @DATA ; Load the address of the data segment
MOV DS, AX ; Move it to DS

; Display the string


MOV DX, OFFSET helloMessage ; Load the address of the message
MOV AH, 09h ; DOS function to display string
INT 21h ; Call DOS interrupt

; Exit the program


MOV AX, 4C00h ; DOS function to terminate the program
INT 21h ; Call DOS interrupt
main ENDP
Assignment 2
Research and Explanation

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.

You might also like