The document provides an introduction to computer organization and assembly language. It discusses that processor understands only machine language instructions as strings of 1s and 0s. Assembly language represents instructions in a more understandable symbolic code for a specific processor family. Assembly language is converted into executable machine code by an assembler utility. Understanding assembly language provides knowledge of how programs interface with the operating system, processor and BIOS, and how instructions access and process data.
2. Introduction
• Processorunderstands only machine language instructions which are strings
of 1sand0s.
• The low level assembly language is designed for aspecific family of
processorsthat represents various instructions in symbolic code and amore
understandable form.
3. What isAssembly
• An assembly language is alow-level programming language for acomputer,
or otherprogrammable device.
• Assembly language is converted into executable machine code by autility
program referred to asanassembler.
4. WhyAssembly?
• An understanding of assembly language provides knowledge of:
• Interface of programswith OS,processorand BIOS.
• Representationof data in memory and other external devices.
• How processor accessesandexecutes instruction.
• How instructionsaccessesandprocessdata
5. WhyAssembly?
• It requires lessmemory and execution time
• It allows hardware-specific complex jobs in an easier way
• It is most suitable for writing interrupt service routines and other memory
resident programs
7. • Byte – sequence of 8 bits:
16 35 4 07 2
MSB (Most Significant Bit) LSB (Least Significant Bit)
• Bit – basic information unit: (1/0)
• Word – a sequence of bits addressed as a single entity by the computer
byte byte 16 bit word
• Main Memory is an array of bytes,
addressed by 0 to 232-1=0xFFFFFFFF
232 bytes = 4∙210∙3 bytes = 4 G bytes
…
…
232-1
2K-1
1
0
address
space
physical
memory
Data Representation Basics
8. DataSize
• Theprocessor supports the following data sizes:
• Word: a2-byte dataitem
• Doubleword: a4-byte (32bit) data item
• Quadword: an8-byte (64 bit) data item
• Paragraph:a16-byte (128bit) area
9. Register file - CPU unit which contains (32 bit) registers.
general purpose registers
EAX, EBX, ECX, EDX
(Accumulator, Base, Counter, Data)
index registers
ESP, EBP, ESI, EDI
(Stack pointer - contains the address of last used
dword in the stack, Base pointer, Source index,
Destination Index)
flag register / status register
EFLAGS
Instruction Pointer / Program Counter EIP / EPC
- contains address (offset) of the next instruction that is going to be executed (at run time)
- changed by unconditional jump, conditional jump, procedure call, and return instructions
Registers
Note that the list of registers above is partial. The full list can be found here.
Register file
High byte Low byteExtended
16-bit register
10. General-purposeregisters
• Some general-purpose registers have specializeduses:
• EAX is automatically usedby multiplication and division instructions.It is often called
the extended accumulatorregister.
• TheCPUautomatically usesECX asaloop counter.
• ESP addresses data on the stack.It is rarely usedfor ordinary arithmetic or data
transfer.It is often calledthe extended stack pointer register.
• ESI and EDI are usedby high-speedmemory transfer instructions.They are sometimes
calledthe extended sourceindex and extended destination index registers.
• EBP is used by high-level languages to reference function parameters and local
variableson the stack.It should not be used for ordinary arithmetic or data transfer
except at anadvanced level of programming.It is often calledthe extended frame
pointer register.
11. SegmentRegisters
• In real-address mode, 16-bit segment registers indicate baseaddresses of
preassigned memory areas namedsegments.
• In protected mode, segment registers hold pointers to segment descriptor
tables.
13. EFLAGSRegister
• TheEFLAGS(or just Flags)register consists of individual binary bits that
control the operation of the CPUor reflect the outcome of some CPU
operation
14. StatusFlags
• The Status flags reflect the outcomes of arithmetic and logical operations
performed by the CPU.They are the Overflow, Sign, Zero, Auxiliary Carry,
Parity, and Carryflags.
15. StatusFlags
• TheCarryflag (CF)is set whenthe result of anunsignedarithmetic operation is too large to
fit into the destination.
• TheOverflow flag (OF)is set when the result of asignedarithmetic operation is too large or
too small to fit into the destination.
• TheSign flag (SF)is set when the result of anarithmetic or logical operation generates a
negative result.
• TheZero flag (ZF)is set when the result of anarithmetic or logical operation generates a
result ofzero.
• TheAuxiliaryCarryflag (AC)is set when anarithmetic operation causesacarryfrom bit 3to
bit 4 in an8-bit operand.
• The Parity flag (PF)is set if the least-significantbyte in the result containsaneven number
of 1bits. Otherwise, PFis clear. In general, it is used for error checking when there is a
possibility that data might be altered or corrupted.
16. Assembly Language Program
• consists of a series of processor instructions, meta-statements, comments, and
data
• translated by assembler into machine language instructions (binary code) that
can be loaded into memory and executed
Example:
assembly code:
MOV AL, 61h ; load AL with 97 decimal (61 hex)
binary code:
10110000 01100001
1011 a binary code (opcode) of instruction 'MOV'
0 specifies if data is byte (‘0’) or full size 16/32 bits (‘1’)
000 a binary identifier for a register 'AL'
01100001 a binary representation of 97 decimal
(97d = (int)(97/16)*10 + (97%16 converted to hex
digit) = 61h)
17. label: (pseudo) instruction operands ; comment
optional fields
either required or forbidden by an
instruction
Notes:
- backslash () : if a line ends with
backslash, the next line is considered
to be a part of the backslash-ended
Examples:
mov ax, 2 ; moves constant 2 to the register ax
buffer: resb 64 ; reserves 64 bytes
Basic Assembly Instruction Structure
• each instruction has its offset (address)
• we mark an instruction with a label to refer it in the code
• (non-local) labels have to be unique
• an instruction that follows a label can be at the same / next line
• colon is optional
…
buffer
RAM
64bytes
2048
mov buffer, 2 = mov 2048, 2
mov [buffer], 2 = mov [2048], 2
mov byte [buffer], 2= mov byte [2048], 2
18. A typical instruction has 2 operands
- target operand (left)
- source operand (right)
3 kinds of operands exists
- immediate : value
- register : AX,EBP,DL etc.
- memory location : variable or pointer
Instruction Arguments
mov [var1],[var2]! Note that x86 processor does not
allow both operands be memory locations.
Examples:
mov ax, 2 mov [buffer], ax
source operandtarget operand
immediateregister
source operandtarget operand
registermemory location
19. mov reg8/mem8(16,32),reg8/imm8(16,32)
(copies content of register / immediate (source) to register / memory location (destination))
mov reg8(16,32),reg8/mem8(16,32)
(copies content of register / memory location (source) to register (destination))
MOV - Move Instruction – copies source to destination
Note that NASM doesn’t remember the types of variables you declare . It will
deliberately remember nothing about the symbol var except where it begins, and
so you must explicitly code mov word [var], 2.
operands have to be
of the same size
Examples:
mov eax, 0x2334AAFF mov [buffer], ax mov word [var], 2
imm32reg32
reg16mem16 imm16mem16
20. ADD - add integers
Example:
add AX, BX ;(AX gets a value of AX+BX)
ADC - add integers with carry
(value of Carry Flag)
Example:
adc AX, BX ;(AX gets a value of AX+BX+CF)
Basic Arithmetical Instruction
<instruction> reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
<instruction> reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
SUB - subtract integers
Example:
sub AX, BX ;(AX gets a value of AX-BX)
SBB - subtract with borrow
(value of Carry Flag)
Example:
sbb AX, BX ;(AX gets a value of AX-BX-CF)
21. Basic Arithmetical Instruction
<instruction> reg8/mem8(16,32)
(source / destination - register / memory location)
INC - increment integer
Example:
inc AX ;(AX gets a value of AX+1)
DEC - increment integer
Example:
dec byte [buffer] ;([buffer] gets a value of [buffer] -1)
22. Basic Logical Instructions
<instruction> reg8/mem8(16,32)
(source / destination - register / memory location)
NOT – one’s complement negation – inverts all the bits
Example:
mov al, 11111110b
not al ;(AL gets a value of 00000001b)
;(11111110b + 00000001b = 11111111b)
NEG – two’s complement negation – inverts all the bits, and adds 1
Example:
mov al, 11111110b
neg al ;(AL gets a value of not(11111110b)+1=00000001b+1=00000010b)
;(11111110b + 00000010b = 100000000b = 0)
23. Basic Logical Instructions
OR – bitwise or – bit at index i of the destination gets ‘1’ if bit at index i
of source or destination are ‘1’; otherwise ‘0’
Example:
mov al, 11111100 b
mov bl, 00000010b
or AL, BL ;(AL gets a value 11111110b)
<instruction> reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
<instruction> reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
AND– bitwise and – bit at index i of the destination gets ‘1’ if bits at index
i of both source and destination are ‘1’; otherwise ‘0’
Example:
or AL, BL ;(with same values of AL and BL as in previous example, AL gets a value 0)
24. cmp reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
cmp reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
CMP – Compare Instruction – compares integers
Examples:
mov al, 11111100b
mov bl, 00000010b
cmp al, bl ;(ZF (zero flag) gets a value 0)
CMP performs a ‘mental’ subtraction - affects the flags as if the subtraction had
taken place, but does not store the result of the subtraction.
mov al, 11111100b
mov bl, 11111100 b
cmp al, bl ;(ZF (zero flag) gets a value 1)