Lab 03 Basics of Assembly Language
Lab 03 Basics of Assembly Language
Lab 03 Basics of Assembly Language
& Assembly
Language Lab
Lab#03 – Basics of Assembly Language
Agenda
Introduction to Assembly Programming ....................................................................................................... 2
Basic Assembly Commands ....................................................................................................................... 2
MOV (Move) .................................................................................................................................... 2
ADD (Addition) ................................................................................................................................. 2
SUB (Subtraction) ............................................................................................................................. 3
MUL (Multiplication): ....................................................................................................................... 3
Data Types in Assembly Language ............................................................................................................ 4
Symbolic Constants ...................................................................................................................... 10
Equal-Sign Directive ..................................................................................................................... 10
1|Page
Introduction to Assembly Programming
Basic Assembly Commands
You should have basic information about general purpose registers according to IA (Intel Architecture).
1. MOV
2. ADD
3. SUB
4. MUL
MOV (Move)
Copies data from a source operand to a destination operand, it is equivalent to assignment operator as
in C/C++. General Format of instruction is as follows:
Syntax
For example
ADD (Addition)
A source operand is added to a destination operand, and the sum is stored in the destination. Operand
must be the same size. General Instruction format is as follows:
Syntax
2|Page
For example
;EAX=635
SUB (Subtraction)
Subtract the source operand from destination operand, and the subtraction is stored the destination.
Operand must be the same size. General Instruction format is as follows:
Syntax
For example
;EAX=389
MUL (Multiplication):
Multiplies AL, AH, AX or EAX by a source operand. i.e. Multiplies the source operand with Accumulator
Register(AC). General Instruction format is as follows:
Syntax
MUL source
3|Page
For example
;EAX=60
For example
myWord WORD ?
myDWord DWORD 5000
Note: Instruction XOR is used to clear the registers contents e.g. XOR EAX, EAX
Getting Started
Use
Call WriteInt ;statement to display the signed integer
Call WriteDec ;statement to display the unsigned integer
Call WriteChar ;statement to display a character
Call WriteString ;statement to display a appropriate message.
Call Crlf ;for new line (CR = Carriage Return (\r), LF = Line Feed (\n))
Call Clrscr ;statement to clear the screen
4|Page
Defining BYTE and SBYTE Data
TITLE Data Definitions
; Examples showing how to define data.
INCLUDE Irvine32.inc
mov al,value3
call WriteDec
call crlf
XOR EAX,EAX
mov al,value4
call WriteDec
call writeInt
call crlf
XOR EAX,EAX
mov al,value5
call WriteDec
call writeInt
call crlf
5|Page
Defining String Data Types
TITLE Data Definitions
; Examples showing how to define data.
INCLUDE Irvine32.inc
.data
.code
main PROC
mov edx, OFFSET Str1
call WriteString
call crlf
xor edx,edx
mov edx, offset str2
call WriteString
exit
main ENDP
END main
Both 0ah and 0dh are hexadecimal values. Hex values can be specified in two ways in assembly - append
an h after the hex value or append the value to 0x.
0ah is equivalent to 10 in decimal and to linefeed ('\n') in ASCII which moves the cursor to the next row
of the screen but maintaining the same column. 0dh is equivalent to 13 in decimal and to carriage return
('\r') in ASCII which moves the cursor to the beginning of the current row. A combination of the two thus
moves the cursor to the beginning of the next row of the screen.
Note:
The OFFSET operator returns the offset address of a variable.
OFFSET: The distance in bytes from the segment address to another location within segment.
6|Page
Using DUP Operator
The DUP operator generates a repeated storage allocation. It is particularly useful when allocating space
for a string or array, and can be used with both initialized and uninitialized data definitions
INCLUDE Irvine32.inc
.data
; ----------------- Usage of DUP Operator ---------------------
.code
main PROC
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
; ----------------- Word Values ---------------------
.code
main PROC
exit
main ENDP
END main
7|Page
; Examples showing how to define data.
INCLUDE Irvine32.inc
.data
; --------------- DoubleWord Values --------------
.code
main PROC
exit
main ENDP
END main
8|Page
Defining Quad-Word and Ten-Byte
INCLUDE Irvine32.inc
.data
; ------- QuadWord and TenByte Values ------------
quad1 DQ 234567812345678h
ten1 DT 1000000000123456789Ah
.code
main PROC
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
; ----------------- Pointers ---------------------
.code
main PROC
exit
main ENDP
END main
9|Page
Symbolic Constants
Equal-Sign Directive
The equal-sign directive associates a symbol name with an integer expression. The Syntax is:
name = expression
INCLUDE Irvine32.inc
.data
COUNT = 10
myarray BYTE COUNT DUP(0)
.code
main PROC
COUNT = 100;
mov eax, count;
call WriteInt
call crlf ; for new line
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
.code
main PROC
10 | P a g e
call WriteDec
call Crlf
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
.code
main PROC
exit
main ENDP
END main
11 | P a g e
Using TEXTEQU Directive
It is very similar to the EQU directive. There are three different formats:
The first assigns text, the second assigns an existing text macro, and the third assigns constant integer
expression.
INCLUDE Irvine32.inc
.data
rowsize = 5
count TEXTEQU %(rowsize*2) ;same as: count TEXTEQU <10>
move TEXTEQU <mov>
setupEAX TEXTEQU <move eax,count>
;same as: setupEAX TEXTQU <move eax, 10>
.code
main PROC
setupEAX
call WriteDec
call Crlf
exit
main ENDP
END main
12 | P a g e