0% found this document useful (0 votes)
83 views49 pages

Chapter Two Basics of Assembly Language Programs: Introduction To Assembly Language. What Is Assembly Language?

Uploaded by

Tekalign zeleke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views49 pages

Chapter Two Basics of Assembly Language Programs: Introduction To Assembly Language. What Is Assembly Language?

Uploaded by

Tekalign zeleke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter Two

Basics of Assembly Language Programs


Introduction to assembly language.
What is Assembly Language?
• Assembly language is a low-level language.
• Assembly language provides directly what the processor provides you.
• The processor does not provide such function like Cin and Cout.

prepared by Berihun G. Msc in computer sc [AAU] 1


Cont…

• So how do we communicate with the user?

• It is simple we can use system call (or interrupt calls in DOS).

• The only solution is code them explicitly in our programs.

Mov al,2h

Mov dl,’a’

Int 21 <--------- coding interrupt explicitly

• Assembly language is the most basic programming language available for any processor.

• With assembly language, a programmer works only with operations implemented


directly on the physical CPU.
prepared by Berihun G. Msc in computer sc [AAU] 2
Assembly language structure/components

prepared by Berihun G. Msc in computer sc [AAU] 3


Cont…

• At one time many assembly language mnemonics were three letter abbreviations,
such as JMP for jump, INC for increment, etc.

• Modern processors have a much larger instruction set and many mnemonics are
now longer, for example FPATAN for "floating point particular tangent” and
BOUND for "check array index against bounds".
prepared by Berihun G. Msc in computer sc [AAU] 4
• In some assembly languages the same mnemonic such as MOV may refer
to a family of related OPCODES for loading, copying and moving data,
whether these are immediate values, values in registers, or memory
locations pointed to by values in registers.

• Other assemblers may use separate OPCODES such as L for "move


memory to register", ST for "move register to memory", LR for "move
register to register", MVI for "move immediate operand to memory",
etc.

prepared by Berihun G. Msc in computer sc [AAU] 5


Comparison of assembly and high-level languages
• Assembly language is low level and High-level language is high level.

• AL can use registers and main memory and HLL uses main memory.

• AL is machine oriented and HLL is human oriented.

• Assembly languages are close to a one to one correspondence between


symbolic instructions and executable machine codes.

• Assembly languages also include directives to the assembler, directives to


the linker, directives for organizing data space, and macros.

prepared by Berihun G. Msc in computer sc [AAU] 6


Typical applications of Assembly language

• Hard-coded assembly language is typically used in a system's boot


ROM (BIOS on IBM-compatible PC systems).

• A stand-alone binary executable of compact size is required, i.e. one that must
execute without recourse to the run-time components or libraries associated
with a high-level language; this is perhaps the most common situation.

• Assembly language is also valuable in reverse engineering, since many


programs are distributed only in machine code form, and machine code is
usually easy to translate into assembly language and carefully examine in this
form, but very difficult to translate into
prepared by Berihun G. Msc a higher-level
in computer sc [AAU] language. 7
Cont…
• In time critical application a specified time period (real time application)
a) Aircraft navigation systems
b) Process control systems
c) Robot control software
d) Communication software
e) Target acquis ion (Missile tracking) software
• System software often requires direct control over the system hardware, So,
it needs assembly program to communicate with the hardware. Such system
software
a) Operating system (low level part of operating
system)
b) Assembler and compiler
c) Linkers and loaders
d) Device driver and network interface

prepared by Berihun G. Msc in computer sc [AAU] 8


What is wrong with Assembly Language?

Here are the reasons people give for not using assembly:

• Assembly is hard to learn.

• Assembly language programming is time consuming and not portable.

• Improved compiler technology has eliminated the need for assembly language.

• Today, machines are so fast and have more memory that we no longer need to
use assembly.

• If you need more speed, you should use a better algorithm rather than switch to
assembly language.
prepared by Berihun G. Msc in computer sc [AAU] 9
Assembly language is:

• Hard to learn

• Hard to read and understand

• Hard to debug

• Hard to maintain

• Hard to write

• Time consuming and not portable

prepared by Berihun G. Msc in computer sc [AAU] 10


What is right with Assembly language?
Assembly language has several benefits:

• Speed. Assembly language programs are generally the fastest programs around.

• Space. Assembly language programs are often the smallest.

• Capability. You can do things in assembly which are difficult or impossible


in HLLs.

• Knowledge. Your knowledge of assembly language will help you write better
programs, even when using HLLs.

prepared by Berihun G. Msc in computer sc [AAU] 11


Hierarchy of Languages
Application Program Positive Vibes:MPI is the interesting, easiest and scoring subject.

• Native to a
High Level Language processor:
Machine Independent High Level Language
executed directly
Machine Specific Low Level Language by hardware
Assembly Language • Instructions
consist of binary
MOV BL,05h Machine Language • Assembly language is code: 1s and 0s
ADD CL
a low-level
MUL BH
programming
language designed for
Micro Program Control a specific type of
processor.
1100 1011 Hardware • Uses symbolic names
1100 1110 to represent
0100 1111 operations, registers
and memory
locations.
Prof. Swati R Sharma Unit 4 – Assembly Language Basics 12
Compilers and Assemblers
Positive Vibes:MPI is the interesting, easiest and scoring subject.
High Level Language

Compiler

Compiler Assembly Language

Assembler

Machine Language

Compilers translate high-level programs to machine code


either directly, or Indirectly via an assembler

Assemblers translates assembly code to machine code


Prof. Swati R Sharma Unit 4 – Assembly Language Basics 13
Tools of Assembly language programming
• Assembly language programming tools are available as free software packages under the
GNU licensing agreement and as commercial software available for purchase.
• To program in assembly, you will need some software, namely an assembler and an
editor.

Assemblers
1. MASM –Originally by Microsoft, it's now included in the MASM32v8 package, which
includes other tools as well.
2. TASM – Another popular assembler. Made by Borland but is still a commercial product, so
you cannot get it for free.
3. NASM –A free, open source assembler, which is also available for other platforms.
Editor:-
4. Emulator software[EMU8086]  Notepad/ notepad ++
prepared by Berihun G. Msc in computer sc [AAU] 14
 Textpad, VS code, Jedit
Basic Elements of Assembly Language
Integer Constants
• An integer constant (or integer literal) is made up of an optional leading
sign, one or more digits, and an optional suffix character (called a radix)
indicating the number’s base: [{+ | −}] digits [ radix]
• Radix may be one of the following (uppercase or lowercase):

If no radix is given, the integer constant is assumed to be decimal. Here are some
examples using different radixes:
prepared by Berihun G. Msc in computer sc [AAU] 15
Continued …
• Example:-

• A hexadecimal constant beginning with a letter must have a leading zero to prevent the
assembler from interpreting it as an identifier.
• Integer Expressions:-is a mathematical expression involving integer values and
arithmetic operators.
• It can store 32 bits (00000000h through FFFFFFFFh).
• The arithmetic operators are listed in the following table according to their precedence
order, from highest (1) to lowest (4).
prepared by Berihun G. Msc in computer sc [AAU] 16
Continued …

The following are valid expressions and values

• Precedence refers to the implied order of


operations when an expression contains two
or more operators.

prepared by Berihun G. Msc in computer sc [AAU] 17


Continued …
• Real number constants:- are represented as Character Constants:
decimal reals or encoded (hexadecimal) reals.  Single character w/c is enclosed in single or double
quotes.
• It contains an optional sign followed by an Example: ‘A’ ,”d”
integer, a decimal point, an optional integer that String Constants:
expresses a fraction, and an optional exponent:
• [sign] integer.[ integer][ exponent]  Is a sequence of characters (including spaces) enclosed in
single or double quotes.
sign {+,-}  Examples:
exponent E[{+,-}] integer  'ABC'
• +3.0  'X'
• -44.2E+05  "Good night, Gracie"
 '4096’
• 26.E5 Embedded quotes:
=>At least one digit and a decimal point  "This isn't a test"
are required.  ‘Say "Good night," Helen’
prepared by Berihun G. Msc in computer sc [AAU] 18
Reserved Words
have special meaning in Assembler and can only be used in their correct context.
There are different types of reserved words:
 Instruction mnemonics, such as MOV, ADD, MUL ,INC, JMP, etc
 Register names, such as AX,BX,CX,SI,DI,
 Directives, which tell assembler how to assemble programs such as .code, .data, .stack
 Attributes, which provide size and usage information for variables and operands.
Examples are BYTE and WORD
 Operators, used in constant expressions such as +/-*

prepared by Berihun G. Msc in computer sc [AAU] 19


Identifiers
• It is a programmer-chosen name. It might identify a variable, a constant, a procedure, or a
code label.
• Keep the following in mind when creating identifiers:
They may contain between 1 and 247 characters.
They are not case sensitive.
The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $.
Subsequent characters may also be digits.
An identifier cannot be the same as an assembler reserved word.
• The @ symbol is used extensively by the assembler as a prefix for predefined symbols, so
avoid it in your own identifiers. Make identifier names descriptive and easy to understand. Here
are some valid identifiers:

prepared by Berihun G. Msc in computer sc [AAU] 20


Assembly Language Directives
Is a command embedded in the source code that is recognized and acted upon by the assembler.
Do not execute at runtime.
Can define variables, macros, and procedures.
They can assign names to memory segments and perform many other housekeeping tasks related to
the assembler.
Directives are case insensitive. For example, it recognizes .data, .DATA, and .Data as equivalent.
The following example helps to show the difference between directives and instructions.
The DWORD directive tells the assembler to reserve space in the program for a doubleword variable.
The MOV instruction, on the other hand, executes at runtime, copying the contents of myVar to the
EAX register:
myVar DWORD 26 ; DWORD directive
MOV EAX, myVar ; MOV instruction

prepared by Berihun G. Msc in computer sc [AAU] 21


Continued …
• Defining Segments:
• One important function of assembler directives is to define program sections,
or segments.
• The .DATA directive identifies the area of a program containing variables: .data
• The .CODE directive identifies the area of a program containing executable
instructions: .code
• The .STACK directive identifies the area of a program holding the runtime
stack, setting its size: .stack 100h
• Instructions :-is a statement that becomes executable when a program is
assembled. It is translated by the assembler into machine language bytes, which
are loaded and executed by the CPU at runtime. An instruction contains four
basic parts:
• [ label:] mnemonic [ operands] [; comment]
1 optional 2 Required 3 required 4 optional
prepared by Berihun G. Msc in computer sc [AAU] 22
Continued …

• Label:-is an identifier that acts as a place marker for instructions and data.
• A label placed just before an instruction implies the instruction’s address. Similarly, a label placed just before a variable
implies the variable’s address.
• Data Labels: A data label identifies the location of a variable, providing a convenient way to reference the variable in code. Example;
 Count DWORD 100
 array DWORD 1024, 2048, 4096, 8192 ; It is possible to define multiple data items following a label.
Code Labels: A label in the code area of a program (where instructions are located) must end with a colon (:).
Are used as targets of jumping and looping instructions.
• For example, the following JMP (jump) instruction transfers control to the location marked by the label named target, creating a
loop:
− target:
− mov ax,bx
− ...
− jmp target
A code label can share the same line with an instruction, or it can be on a line by itself:
− L1: mov ax,bx
− L2:
prepared by Berihun G. Msc in computer sc [AAU] 23
Continued …

• Operands: Assembly language instructions can have between zero and three
operands, each of which can be a register, memory operand, constant
expression Comments: Descriptions
1. Single-line comments, beginning with a
semicolon character (;).
2. Block comments, beginning with the
COMMENT directive and a user-specified
symbol.
COMMENT ! This is comment line ! Or
COMMENT & comment line &

• The STC instruction, for example, has no operands: stc ; set Carry flag
• The INC instruction has one operand: inc eax ; add 1 to EAX
• The MOV instruction has two operands: mov count,ebx ; move EBX to count
• The imul instruction has Three operands: imul eax,ebx,5 ;In this case, EBX is multiplied by 5,
and the product is stored in the prepared
EAXbyregister.
Berihun G. Msc in computer sc [AAU] 24
Assembling, Linking, and Running Programs
• A source program written in assembly language cannot be executed directly on
its target computer. It must be translated, or assembled into executable code.
• The assembler produces a file containing machine language called an object
file. This file isn’t quite ready to execute. It must be passed to another program
called a linker, which in turn produces an executable file.

prepared by Berihun G. Msc in computer sc [AAU] 25


Defining Data Types
• It describes a set of values that can be assigned to variables and expressions
of the given type.
• The essential characteristic of each type is its size in bits: 8, 16, 32, 48, 64
• A variable declared as DWORD, for example, logically holds an unsigned 32-
bit integer. In fact, it could hold a signed 32-bit integer, a 32-bit single
precision real, or a 32-bit pointer. The assembler is not case sensitive, so a
directive such as DWORD can be written as dword, Dword, dWord, and so on.
Instruction Mnemonic
• Is a short word that identifies an instruction. Assembly language instruction
mnemonics such as mov, add, and sub provide hints about the type of
operation they perform.

prepared by Berihun G. Msc in computer sc [AAU] 26


Following are examples of instruction mnemonics:

prepared by Berihun G. Msc in computer sc [AAU] 27


Data Definition Statement

• Syntax: name directive initializer [, initializer]...


count DWORD 12345
Directive: The directive in a data definition statement can be BYTE,
WORD, DWORD, SBYTE, SWORD, or DB,DW,DD,DQ.
Initializer: At least one initializer is required in a data definition, even
if it is zero. Additional initializers, if any, are separated by commas.
For integer data types, initializer is an integer constant or expression
matching the size of the variable’s type, such as BYTE or WORD.
If you prefer to leave the variable uninitialized (assigned a random
value), the ? symbol can be used as the initializer. All initializers,
regardless of their format, are converted to binary data by the
assembler.
prepared by Berihun G. Msc in computer sc [AAU] 28
Defining BYTE and SBYTE Data
• The BYTE (define byte) and SBYTE (define signed byte) directives allocate
storage for one or more unsigned or signed values. Each initializer must fit
into 8 bits of storage. For example:- The optional name is a label marking the
value1 BYTE 'A' ; character constant variable’s offset from the beginning of its
value2 BYTE 0 ; smallest unsigned byte enclosing segment. For example, if value1 is
value3 BYTE 255 ; largest unsigned byte located at offset 0000 in the data segment
value4 SBYTE −128 ; smallest signed byte and consumes 1 byte of storage, value2 is
value5 SBYTE +127 ; largest signed byte automatically located at offset 0001:
• value1 BYTE 10h
• A question mark (?) initializer leaves the
• value2 BYTE 20h
variable uninitialized, implying it will beThe DB directive can also define an 8-bit
assigned a value at runtime: variable, signed or unsigned:
• val1 DB 255 ; unsigned byte
value6 BYTE ?
• val2 DB -128 ; signed byte
prepared by Berihun G. Msc in computer sc [AAU] 29
Multiple Initializers
• If multiple initializers are used in the same data definition, its label refers
only to the offset of the first initializer. In the following example, assume list
is located at offset 0000. If so, the value 10 is at offset 0000, 20 is at offset
0001, 30 is at offset 0002, and 40 is at offset 0003:
list BYTE 10,20,30,40
• list BYTE 10,20,30,40 BYTE 50,60,70,80
BYTE 81,82,83,84;multiple declaration
Within a single data definition, its initializers can use
different radixes. Character and string constants can be
freely mixed.
In the following example, list1 and list2 have the same
contents:
• greeting1 BYTE "Good afternoon",0  list1 BYTE 10, 32, 41h, 00100010b
• greeting2 BYTE 'Good night',0  list2 BYTE 0Ah, 20h, 'A', 22h
• Each character uses a byte of storage. Strings are an exception to the rule that byte30
prepared by Berihun G. Msc in computer sc [AAU]
DUP Operator
• The DUP operator allocates storage for multiple data items, using a constant
expression as a counter. It is particularly useful when allocating space for a string
or array, and can be used with initialized or uninitialized data:
• BYTE 20 DUP(0) ; 20 bytes, all equal to zero
• BYTE 20 DUP(?) ; 20 bytes, uninitialized
• BYTE 4 DUP("STACK") ; 4 bytes: " STACKSTACKSTACKSTACK "
– .data Data with instruction
– smallArray DWORD 10 DUP(0) ; 40 bytes – .code
– .data? – mov eax,ebx
– bigArray DWORD 5000 DUP(?) ; 20,000 bytes, not initialized – .data
– .data – temp DWORD ?
– smallArray DWORD 10 DUP(0) ; 40 bytes – .code
– bigArray DWORD 5000 DUP(?) ; 20,000 bytes – mov temp,eax
prepared by Berihun G. Msc in computer sc [AAU] 31
Data Transfer Instructions
• Label is optional and can be ignored. Operands can be zero, one, two, three
• mnemonic ;no operands
• mnemonic [destination] ;one operands
• mnemonic [destination],[ source] ; two operands
• mnemonic [destination],[ source-1],[ source-2] ; three operands
• x86 assembly language uses different types of instruction operands
• Immediate—uses a numeric literal expression
• Register—uses a named register in the CPU
• Memory—references a memory location

prepared by Berihun G. Msc in computer sc [AAU] 32


Simple notation for operands

prepared by Berihun G. Msc in computer sc [AAU] 33


MOV Instruction
• The MOV instruction copies data from a source operand to a destination
operand.
• Known as a data transfer instruction, it is used in virtually every program
• Syntax:- MOV destination, source
• The destination operand’s contents change, but the source operand is
unchanged. The right to left movement of data is similar to the assignment
statement in C++ or Java:dest = source;
• (In nearly all assembly language instructions, the left-hand operand is the
destination and the righthand operand is the source.)
• MOV is very flexible in its use of operands, as long as the following rules are
observed:
prepared by Berihun G. Msc in computer sc [AAU] 34
Cont’d….
Both operands must be the same size.
Both operands cannot be memory operands.
CS, EIP, and IP cannot be destination operands. Overlapping Values:-
An immediate value cannot to a segment register. .data
oneByte BYTE 78h
Here is a list of the general variants of MOV
oneWord WORD 1234h
• MOV reg,reg .data oneDword DWORD 12345678h
• MOV mem,reg
var1 WORD ? .code
• MOV reg,mem
• MOV mem,imm var2 WORD ? mov eax,0 ; EAX = 00000000h
mov al,oneByte ; EAX = 00000078h
• MOV reg,imm .code mov ax,oneWord ; EAX = 00001234h
• MOV reg/mem16,sreg mov ax,var1 mov eax,oneDword ; EAX =
mov var2,ax 12345678h
• MOV sreg,reg/mem16 prepared by Berihun G. Msc in computermov ax,0 ; EAX = 12340000h 35
sc [AAU]
Addition and subtraction
• There are different types of arithmetic instruction sets. Let’s begin with INC (increment), DEC
(decrement), ADD (add), SUB (subtract), and NEG (negate).
INC and DEC Instructions
• The INC (increment) and DEC (decrement) instructions, respectively, add 1 and subtract 1 from
a single operand. ADD Instruction
• The syntax is: Syntax: ADD dest, source
• INC reg/mem .data
• DEC reg/mem
var1 dw 10000h
• Following are some examples: var2 dw 20000h
– .data .code
– myWord dw 1000h mov eax,var1 ; EAX = 10000h
– .code add eax,var2 ; EAX = 30000h
– inc myWord ; myWord = 1001h  Flags :-The Carry, Zero, Sign, Overflow, Auxiliary
– mov bx,myWord Carry, and Parity flags are changed according to
– dec bx ; BX = 1000h the value that is placed in the destination operand.
prepared by Berihun G. Msc in computer sc [AAU] 36
Cont’d…..
• SUB dest, source
• Here is a short code example that subtracts two 32-bit integers:
– org 100h
– .data
– var1 DD 300h
– var2 DD 100h
– .code
–mov ax,var1; AX = 300h
–sub ax,var2 ; AX = 200h
– Ret
– Flags The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to
the value that is placed in the destination operand.
• NEG Instruction:
• The NEG (negate) instruction reverses the sign of a number by converting the number to its two’s complement. The
following operands are permitted:
• NEG reg
• NEG mem prepared by Berihun G. Msc in computer sc [AAU] 37
Div Instruction
• It has one destination operand
• Synatx: Div S ;if S=8bit then Ax/S8 is possible to operate
 Q(Ax/S8)==> AL,R(Ax/S8)==> AH
 if S=16bit (DX,AX)/S16 is possible to operate
1. .code  Q(DX,Ax)/S16==> AX,R(DX,AX)/S16==> DX
2. ; 5001d/25d Q=200,R=1
3. ; 1000d/100d Q=10,R=0
4. ; 55436d/568d ,Q=217,R=284 • mov Ax,1004h
5. mov Ax,1389h • mov dx,1004h
6. mov Bl,19h
• mov Bx,1234h
7. div bl
8. mov Ax,1000 • div Bx ;10041004h/1234h
9. mov Bx,100
10. div Bx ;10 prepared by Berihun G. Msc in computer sc [AAU] 38
Mult instruction
• The default 8 and 16 bit storage while performing multiplication is stored in
accumulative register and 32bit is stored in AX, and DX (16,16)bit registers
• Syntax: Mul S
• Mul D8 ;D8*AL==> AX
;D16*AX==> Dx,Ax
1  mov AL,14h
 mov BL,24h 3 mov Ax,10
 MUL BL ;AH=02 AL=D0 mov BX,10
MUL BX ;AX=0Ah
2  mov Ax,1234h
 mov BX,45F4h
 MUL BX ;AX=5D90,DX=04F9
prepared by Berihun G. Msc in computer sc [AAU] 39
JMP and LOOP Instructions
• Assembly language programs use conditional instructions to
implement high-level statements such as IF statements and loops.
Unconditional Transfer: Control is transferred to a new location in
all cases; a new address is loaded into the instruction pointer,
causing execution to continue at the new address. The JMP
instruction does this.
Conditional Transfer: The program branches if a certain condition
is true. A wide variety of conditional transfer instructions can be
combined to create conditional logic structures.
The CPU interprets true/false conditions based on the contents of
the ECX and Flags registers.
prepared by Berihun G. Msc in computer sc [AAU] 40
Continued …
• JMP destination
top:
jmp top ; repeat the endless loop
• JMP is unconditional, so a loop like this will continue endlessly
unless another way is found to exit the loop.
• org 100h
• .data
• var1 DB "Hello World$"
• .code
• top: • Output infinite loop
• mov ax,@data of displaying the
• mov ds,ax “hello world string”
• mov dx, offset var1
• mov ah,09h
• int 21h
• jmp top prepared by Berihun G. Msc in computer sc [AAU] 41
LOOP Instruction
• ECX is automatically used as a counter and is decremented each time the loop repeats. Its syntax
is LOOP destination
• The execution of the LOOP instruction involves two steps:
• First, it subtracts 1 from ECX. Next, it compares ECX to zero.
• If ECX is not equal to zero, a jump is taken to the label identified by destination. if ECX equals
zero, no jump takes place, and control passes to the instruction following the loop.
• In the following example, we add 1 to AX each time the loop repeats. When the loop ends, AX =
51 and ECX = 0: 2
1. .data
2. sum dw 1
• mov ax,0 3. limit dw 5
• mov cx,5 4. .code
5. mov ax,0
• L1: 6. mov cx,limit
7. lable1:
• inc ax 8. ADD ax,sum
• loop L1 9. inc sum
10. loop lable1 ;output 0Fh,15d,1111b,17o
prepared by Berihun G. Msc in computer sc [AAU] 42
Nested Loops
• When creating a loop inside another loop, special consideration must be given to the
outer loop counter in ECX. You can save it in a variable: 1. org 100h
• .data 2. .data
3. count DW ?
• count DWORD ? 4. .code
5. mov cx,5 ; set outer loop count
• .code 6. L1:
• mov ecx,100 ; set outer loop count 7. mov count,cx ; save outer loop count
8. mov cx,4 ; set inner loop count
• L1: 9. L2:
• mov count,ecx ; save outer loop count 10. mov ah,02h
11. mov dl,2Ah
• mov ecx,20 ; set inner loop count 12. int 21h
• L2: 13. loop L2 ; repeat the inner loop
14. mov cx,count ; restore outer loop
• loop L2 ; repeat the inner loop count
• mov ecx,count ; restore outer loop count 15. mov dl,0Dh ;line feed
16. int 21h
• loop L1 ; repeat the outer loop 17. mov dl,0Ah ;to Disp characters
prepared by Berihun G. Msc in computer sc [AAU] 18. int 21h 43
19. loop L1 ; repeat the outer loop
Arrays
• Arrays can be seen as chains of variables. A text string is an example
of a byte array, each character is presented as an ASCII code value
(0..255).
Here are some array definition examples:
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
b DB 'Hello$’
When compiler sees a string inside quotes it automatically converts it
to set of bytes. This chart shows a part of the memory where these
arrays are declared:

prepared by Berihun G. Msc in computer sc [AAU] 44


Continued …
• You can access the value of any element in array using square brackets, for
example:
MOV AL, a[3]
• You can also use any of the memory index registers:BX, SI, DI, BP for example:
MOV SI, 3
MOV AL, a[SI]
• If you need to declare a large array you can use DUP operator.
The syntax for DUP:
number DUP (value(s))
• number - number of duplicate to make (any constant value).
• value - expression that DUP will duplicate.
for example:
c DB 5 DUP(9) is an alternative way of declaring:
c DB 9, 9, 9, 9, 9

prepared by Berihun G. Msc in computer sc [AAU] 45


Continued…
• LEA (Load Effective Address) instruction and alternative OFFSET operator are used to
access the elements of an array. Both OFFSET and LEA can be used to get the offset
address of the variable.
• LEA is more powerful because it also allows you to get the address of an indexed variables.
Getting the address of the variable can be very useful in some situations, for example when
you need to pass parameters to a procedure.
1. org 100h 1. org 100h
2. MOV AL, VAR1 ; check value of VAR1 by moving it to AL. 2. MOV AL, VAR1.
3. LEA BX, VAR1 ; get address of VAR1 in BX. 3. MOV BX, OFFSET VAR1
4. MOV BYTE PTR [BX], 44h ; modify the contents of VAR1. 4. MOV BYTE PTR [BX], 44h
5. MOV AL, VAR1 ; check value of VAR1 by moving it to AL. 5. MOV AL, VAR1
6. VAR1 DB 22h 6. RET
7. ret 7. VAR1 DB 22h

Line number 3 LEA vs offset same functionality

prepared by Berihun G. Msc in computer sc [AAU] 46


Continued …
• In the next example, arrayB contains 3 bytes. As ESI is incremented, it points to
each byte, in order: • If we use an array of 16-bit integers, we add 2 to
1
1. .data ESI to address each subsequent array element:
2
2. arrayB DB 10h,20h,30h 1. org 100h
3. .code 2. .data
4. mov si,OFFSET arrayB 3. arrayW DW 1000h,2000h,3000h
4. .code
5. mov al,[si] ; AL = 10h 5. mov si, offset arrayW
6. inc si 6. mov ax,[si]; AX = 1000h
7. mov al,[si] ; AL = 20h 7. add si,2
8. mov ax,[si]; AX = 2000h
8. inc si
9. add si,2
9. mov al,[si] ; AL = 30h 10.mov ax,[si]; AX = 3000h
10.ret 11.ret
prepared by Berihun G. Msc in computer sc [AAU] 47
Continued…
• Example: Adding 32-Bit Integers: The following code example adds three double
words. A displacement of 4 must be added to ESI as it points to each subsequent array
value because Double words are 4 bytes long:
–.data
–arrayD DWORD 10000h,20000h,30000h
–.code • Shows the initial value of ESI in
–mov esi,OFFSET arrayD relation to the array data:
–mov eax,[esi] ; first number
–add esi,4
–mov eax,[esi] ; second number
–add esi,4
–mov eax,[esi] ; third number
–Suppose arrayD is located at offset 10200h.
prepared by Berihun G. Msc in computer sc [AAU] 48
The end ?

prepared by Berihun G. Msc in computer sc [AAU] 49

You might also like