Computer Organization and Assembly Language: Muhammad Usman
Computer Organization and Assembly Language: Muhammad Usman
Assembly Language
Muhammad Usman
FUUAST, Islamabad 2
Continue…
• Relation with C/C++
FUUAST, Islamabad 3
ALIGN Directive
• Aligns a variable on a:
– Byte, word, doubleword, paragraph boundary
• Syntax: ALIGN bound
• Where bound can be 1, 2, 4, or 16
• Value of 1 aligns the next variable on a 1-
byte boundary
• If bound is 2, the next variable is aligned
on an even-numbered address
FUUAST, Islamabad 4
Continue…
• Assembler can insert one or more empty
bytes before the variable to fix the alignment
bVal BYTE ? ; 00404000
ALIGN 2
wVal WORD ? ; 00404002
bVal2 BYTE ? ; 00404004
ALIGN 4
dVal DWORD ? ; 00404008
dVal2 DWORD ? ; 0040400C
FUUAST, Islamabad 5
Why bother aligning data?
• Because the CPU can process data stored
at even numbered addresses more quickly
than those at odd-numbered addresses
FUUAST, Islamabad 6
PTR Operator
• Assembly instructions require operands to be of
same size
• It may be required at some point to operate on
data in a size other than that originally declared
• This can be done with the PTR operator.
.data
num DWORD 0
.code
mov ax, WORD PTR num ;Loads a word-size
value from a doubleword variable
FUUAST, Islamabad 7
Continue…
• Overrides default size of operand’s address
• It is useful when operand’s size is not clear
from the context:
inc [bx] ; operand size error
• Can be fixed as:
inc byte ptr [bx]
FUUAST, Islamabad 8
Little Endian Order
• All data types larger than a byte store their
individual bytes in reverse order
• Least significant byte occurs at the first
(lowest) memory address
FUUAST, Islamabad 9
Continue…
Double Word Word Byte Offset
12345678 5678 78 0000 myData
56 0001 myData + 01
1234 34 0002 myData + 02
12 0003 myData + 03
.data
myData DWORD 12345678h
.code
mov ax, myData ; error ?
mov ax, WORD PTR myData ; AX = 5678h
mov ax, WORD PTR [myData+2] ; AX = 1234h
mov al, BYTE PTR myData ; AL = 78h
mov al, BYTE PTR [myData+1] ; AL = 56h
mov al, BYTE PTR [myData+2] ; AL = 34h
FUUAST, Islamabad 10
Indirect Operands
• Holds the address of a variable
• Usually an array or string
.data
val1 BYTE 10h, 20h, 30h
.code
mov esi, OFFSET val1
mov al, [esi] ; AL = 10h,
FUUAST, Islamabad 11
LENGTHOF Operator
• Counts the number of elements in a single
data declaration
.data ;LENGTHOF
byte1 BYTE 10, 20, 30 ;3
array1 WORD 30 DUP(?), 0, 0 ; 32
array2 WORD 5 DUP (3 DUP(?)) ; 15
array3 DWORD 1, 2, 3, 4 ;4
dStr BYTE “PAKISTAN", 0 ;9
.code
mov ecx, LENGTHOF array1 ;32
FUUAST, Islamabad 12
SIZEOF Operator
• Returns a value that is equivalent to
multiplying LENGTHOF by TYPE
.data ;SIZEOF
byte1 BYTE 10, 20, 30 ;3
array1 WORD 30 DUP(?), 0, 0 ; 64
array2 WORD 5 DUP (3 DUP(?)) ; 30
array3 DWORD 1, 2, 3, 4 ; 16
dStr BYTE “PAKISTAN", 0 ;9
.code
mov ecx, SIZEOF array1 ;64
FUUAST, Islamabad 13
Data Spanning Multiple Lines
• Declaration can span multiple lines
• If each line (except the last) ends with a comma
• LENGTHOF and SIZEOF operators include all lines
belonging to the declaration
.data
array WORD 10,20,
30,40,
50,60
.code
mov eax, LENGTHOF array ;6
mov ebx, SIZEOF array ; 12
FUUAST, Islamabad 14
Array
• Reserves consecutive memory space
NUMBERS WORD 34, 45, 56, 67, 75, 89
• Declares an array of six words each
initialized
• Allocates 2x6 = 12 bytes of consecutive
memory space
• Symbolic address of 1st number will be
NUMBERS and that of 2nd number will be
NUMBERS + 2 and so on
FUUAST, Islamabad 15
Continue…
.data
arrayW WORD 1000h, 2000h, 3000h
.code
mov esi, OFFSET arrayW ; ESI = the
address of Val1
mov ax, [esi] ; AX = 1000h
add esi, 2 ;
add ax, [esi] ; AX = 3000h
add esi, 2
add ax, [esi] ; AX = 6000h
FUUAST, Islamabad 16
DUP Operator
• Duplicates variable
• Use DUP to allocate an array or string
• Counter and argument must be constants
or constant expressions
var1 BYTE 20 DUP(0) ;20 bytes, all 0’s
var2 BYTE 20 DUP(?) ;20 bytes, uninitialized
var3 BYTE 4 DUP(“STACK”) ;20 bytes
;“STACKSTACKSTACKSTACK”
FUUAST, Islamabad 17
JMP Instruction
• Causes an unconditional transfer to a
destination
• Transfers control to a different point
usually within the same procedure
• Syntax: JMP target
• Here, target is a label where control needs
to be transferred
FUUAST, Islamabad 18
Continue…
top:
.
.
jmp top
• JMP is unconditional, so it will continue
endlessly
FUUAST, Islamabad 19
LOOP Instruction
• Creates counting iterations
• Repeats a block of statements a specific
number of times
• Known as Loop According to ECX Counter
• Syntax: LOOP target
• ECX is automatically used as a counter
• ECX is decremented each time the loop
repeats
FUUAST, Islamabad 20
Continue…
• Loop destination must be within -128 to
+127 bytes of current location counter
• Execution of loop involves two steps:
– It subtracts 1 from ECX
– Compare ECX to zero
– If ECX is not zero, conditional jump is taken to
label
– Otherwise, no jump takes place
FUUAST, Islamabad 21
Continue…
mov ax, 0
mov ecx, 5
L1:
inc ax
loop L1
• When the loop ends,
– AX 5 and ECX 0
FUUAST, Islamabad 22
Common Errors in Loop
• Initialize ECX to zero
– The LOOP instruction decrements ECX by 1
– The ECX is loaded with FFFF FFFFh
– The loop repeats 4,294,967,296 times!
• Explicitly modify ECX inside a loop
– Loop may not work as expected
top:
.
.
inc ecx
loop top ; LOOP never ends, increments ECX
FUUAST, Islamabad 23
Nested Loop
• A loop within another loop
• Special consideration must be given to the
outer loop counter in ECX
• Save the value of ECX in some variable
• Example:
FUUAST, Islamabad 24
Continue…
.data
count DWORD ?
.code
mov ecx, 10 ; set outer loop count
L1:
mov count, ecx ; save outer loop count
.
.
FUUAST, Islamabad 25
Link Library
• A file containing procedures or
subroutines that have been complied into
machine code
• Created using one or more object files
• Suppose we have a program to display a
string on console:
WriteString PROTO
call WriteString
FUUAST, Islamabad 26
Continue…
• When the program is assembled
Assembler leaves the target address of the
CALL instruction blank
• It will be filled in by the linker
• Linker looks for WriteString in the link
library
• Linker copies the appropriate machine
instructions from the library into the
program’s executables file
FUUAST, Islamabad 27
Continue…
• Linker also inserts WriteString’s address
into the CALL instruction
• Linker Utility
– Combines a program’s object file with one or
more object files and link libraries
– Following command, e.g., links hello.obj to the
irvine32.lib and kernel32.lib libraries:
link hello.obj irvine32.lib kernel32.lib
FUUAST, Islamabad 28
Procedures
• Functions are called procedures or
subroutines
• Identified by a unique name
• The PROC and ENDP directives indicate
the start and end of a procedure
• Both the PROC and ENDP directives
require a label to indicate the name of the
procedure
FUUAST, Islamabad 29
PROC Directive …
• Syntax:
<Proc-name> PROC
..
..
RET
<Proc-name>ENDP
FUUAST, Islamabad 30
Continue…
• Procedures use a PROC directive
• ENDP directive to end the construct
• CALL instruction links to a procedure and
• RET instruction returns from a procedure
FUUAST, Islamabad 31
CALL & RET Instructions
• CALL instruction calls the procedure
– Pushes offset of next instruction into stack
– Copies address of called procedure into EIP
• RET instruction returns from the procedure
– Pops top of stack into EIP
FUUAST, Islamabad 32
Continue…
main PROC
..
call MySub
..
main ENDP
MySub PROC
..
ret
MySub ENDP
FUUAST, Islamabad 33