Assembly Language
Assembly Language
Welcome
To assembly(ASM) language programming. The assumption/pre-requisites for this course are computer organization
and architecture and Introduction to programming. The course is organized into thirteen(13) lessons. We will first
focus on the CPU components: the control unit, ALU, registers and the clock then look at the assembly language. In
particular, the course will help you understand:
• The architecture of the registers and how they work.
• Addressing modes in the registers.
• Assembly languages and assemblers.
• How functions/procedures work.
• How control flow works at a most fundamental level.
• How to get more control over the system's resources
For the practical sessions, you will need to download and install an IDE SASM.
For windows: https://github.com/Dman95/SASM/releases/download/v3.10.1/SASMSetup3101.exe
For Linux: http://download.opensuse.org/repositories/home:/Dman95/
Office hours: Fridays 2-4pm
Email: [email protected]
Course outline
Task# Description
1 Introduction
2 Register architecture
3 Assembly languages
4 Assembly language programming tools
5 Address modes
6 Assembly language elements
7 Assembly language program structure
8 Arithmetic operations
9 Control structures
10 Bit Manipulations
11 Masking operations
12 Arrays
13 Procedures
5/19/2019 Kenga©2019 Lecture notes 2
14 References
BSD 1308: Assembly language programming
1. Introduction
Lesson outline:
₋ Overview
₋ Motivation
₋ Definitions
₋ Advantages of assembly languages
₋ Disadvantages of assembly languages
₋ General steps in creating assembly language programs.
₋ Revision questions
1. Introduction
Overview:
Assembly language is a low-level programming language which is device specific (meant for specific
computer architecture).
Assembly language is converted into executable machine code by a utility program referred to as an
assembler.
Each family of processors has its own set of instructions(machine language instructions) for handling
various operations.
Assembly language(ASL) is almost like machine language but it uses words in place of numbers.
Assembly languages(ASL) are often referred to as ‘symbolic machine codes’.
Motivation:
Knowledge, of the underlying hardware, is the key to writing efficient software today.
Assembly language forces the programmer to consider how the underlying hardware operates with
each machine instruction they write.
Thus the key to learning how to write efficient code is focussing on the low-level capabilities of the
5/19/2019
machine. Kenga©2019 Lecture notes 4
BSD 1308: Assembly language programming
1. Introduction
Basic terms
Algorithm: A set of instructions or rules designed to solve a definite problem.
Program: A set of instructions, which can be decoded and executed by the CPU to perform a specific task or
function.
Kernel: Component of the operating system used to manage the communication between the software (user
applications) and the hardware (CPU, disk memory etc.).
Code: Also known as source code is a written set of instructions, created using the protocols of a particular
language.
Operand: The objects which can be manipulated using different operators.
Operator: The object which can manipulate different operands.
Mnemonic: Abbreviations that help remember facts or commands or operations.
Variable: A named location which stores temporary data within a program which can be modified, stored and
displayed whenever needed.
Bit: The smallest unit of data on a computer e.g. 0 or 1.
Nibble: A collection of four bits.
Byte: A collection of eight bits and is the smallest addressable data item.
Word: A group of 16 bits
5/19/2019 Kenga©2019 Lecture notes 5
BSD 1308: Assembly language programming
1. Introduction
Advantages of assembly language
• Provides better control over the hardware. It is the only programming language that can
best expose the processor's power.
• It is possible to write programs that take up little space in small size memory.
• Written programs run faster. Because they work very fast, speed is required for critical
applications in the programming of kernel and hardware drivers in operating systems.
• Under any programming language, it can be used between the language codes.
• C has been written in assembly language
Disadvantages of assembly language
• To write programs in assembly language, the internal structure of the microprocessor must
be known.
• Assembly language varies by microprocessor type. A program written for a microprocessor
may not work on another microprocessor.
• Programming in assembly language is more difficult and time consuming than high-level
5/19/2019
languages. Kenga©2019 Lecture notes 6
BSD 1308: Assembly language programming
1. Introduction
General steps in creating ASM programs:
1. Creating the source code: Using an IDE (e.g. NASM, MASM, GAS, FASM etc), write the
source code. Save the source file with .asm extension.
2. Assembling the source file: Assembly code is converted into executable machine code by
a utility program referred to as an assembler.
3. Run the object file: Using appropriate command, execute the object file
1. Introduction
Revision questions.
a) Explain two advantages and two advantages of assembly language.
b) Briefly explain the procedure for writing an assembly program.
c) Describe the following concepts as used in assembly language programming.
i. Algorithm
ii. Source code
iii. Program
iv. Symbols
v. Variables
vi. Operands
vii. Operators
2. Register architecture
Lesson outline:
• Overview
• Types of CPU registers
• Segmented memory model
• Advantages of registers
• Disadvantages of registers
• Revision questions
2. Register architecture
Overview:
Register: Fast computer memory, used to store data/instruction in-execution.
A group of flip-flops with each flip-flop capable of storing one bit of information.
A register consists of a group of flip-flops and gates.
The flip-flops hold the binary information and gates control when and how new information is transferred into a
register.
Registers are normally measured by the number of bits they can hold e.g. 8-bit register, a 16-bit register or a 32-bit
register
Register file: This is an array of registers in a central processing unit (CPU). Also defined as a component that contains all
the general purpose registers of the microprocessor. Register file is treated as a group of registers.
Flag register: These are a modified kind of register that record the condition of a microprocessor's calculation. The status
of each flag determines the microprocessor's next action, thus enabling it to make decisions. Can also be defined as a It is
a value that acts as a signal for a method or process. Its value is used to determine the next step of a program.
Clock: A particular wire in a CPU that turns on and off in a steady state to help keep everything in sync.
• The clocks in modern CPU turns on and off in several billions per second.
• This speed allows the CPU to do several things quickly.
2. Register architecture
2. Register architecture
• Loading register: The transfer of new information into a register.
• Register transfer language: The symbolic notation used to describe the micro-operation transfers amongst registers.
• Register transfer: The availability of hardware logic circuits that can perform a stated micro-operation and transfer the result of the
operation to the same or another register.
• Types of CPU registers: Broadly classified into two categories
General purpose registers: This is used to store intermediate results during program execution. Mainly for storing data and addresses.
General purpose registers are further classified into data, pointer and index registers. The 32-bit general purpose registers have names
starting with “E”.
a). Data registers: General purpose registers used for used for arithmetic, logical, and other operations.
• EAX: Accumulator register used to store data taken out from the memory.
• EBX: Base register used to perform arithmetic and data movement and it has some special addressing abilities.
• ECX: Counter register is used as counter for repeating or looping instructions.
• EDX: Data register has a special role in multiply and divide operations. It works like a buffer and holds anything that is copied
from the memory ready for the processor to use it
b). Pointer registers: These are general purpose registers that point to/store the memory address.
• EBP- stack base pointer register holds the base address of the stack.
• ESP- Stack pointer register holds the top address of the stack.
• EIP- Stack instruction pointer holds the offset address of the next instruction to be executed.
2. Register architecture
General Purpose Registers………………….continued
c). Index registers: These are general purpose registers used for indexed addressing and sometimes
arithmetic operations.
• EDI- Destination index register used as a destination index for string operations.
• ESI- Source index register is used as source index for string operations.
Special Purpose Registers: Users do not access these registers. These registers are for Computer
system. Mainly to handle arithmetic and logic operations.
• MAR: Memory Address Register holds the address for memory unit.
• MBR: Memory Buffer Register stores instruction and data received from the memory and sent
from the memory.
• PC: Program Counter points to the next instruction to be executed.
• IR: Instruction Register holds the instruction to be executed.
2. Register architecture
Segmented memory model
The model divides the system memory into groups of independent segments (sections).
Each segment is used to contain a specific type of data(instruction codes, data elements, program
stack).
We can specify various memory segments/sections as −
a) Data segment − It is represented by .data section and the .bss. The .data section is used to declare
the memory region, where data elements are stored for the program. This section cannot be
expanded after the data elements are declared, and it remains static throughout the program. The
.bss section is also a static memory section that contains buffers for data to be declared later in the
program. This buffer memory is zero-filled.
b) Code segment − It is represented by .text section. This defines an area in memory that stores the
instruction codes. This is also a fixed area.
c) Stack segment − This segment contains data values passed to functions and procedures within the
program.
2. Register architecture
Advantages of registers
–Faster than cache or main memory (no addressing mode or tags)
–Deterministic (no misses)
–Can replicate (multiple read ports)
–Short identifier (typically 3 to 8 bits)
–Reduce memory traffic
Disadvantages of registers
–Need to save and restore on procedure calls and context switch.
–Fixed size (can’t store strings or structures efficiently)
–Compiler must manage
–Limited number
2. Register architecture
Processor width
• Processors can be identified by two main parameters: width and speed. The processor width can be defined as
the maximum amount of data(bits) that can be processed and delivered at one time. The processor speed refer
to the speed of the processor's internal clock( cycles).
8-bit,16-bit,32-bit and 64-bit processors:
• The number of bits determine the width of the data bus and size of the registers.
• 8-bit processor means that the CPU has 8-bit registers and can write 8-bits to the data bus at one go.
• 16-bit processor means that the CPU has 16-bit registers and can write 16-bits to the data bus at one go.
• 32-bit processor means that the CPU has 32-bit registers and can write 32-bits to the data bus at one go.
• 64-bit processor means that the CPU has 64-bit registers and can write 64-bits to the data bus at one go.
See:
• 8-Bits=11111111
• 16-bits=1111111111111111
• 32-bits=11111111111111111111111111111111
• 64-bits=1111111111111111111111111111111111111111111111111111111111111111
2. Register architecture
X86 general purpose registers.
• The 64-bit registers have names beginning with "r". The 32-bit registers (lower 32 bits) names begin with “e”.
The new registers are named r8 through r15.The following table specifies the assembly-language names for the
lower portions of 64-bit registers.
64-bit register Lower 32 bits Lower 16 bits Lower 8 bits
RAX EAX AX AL
RBX EBX BX BL
RCX ECX CX CL
RDX EDX DX DL
RSI ESI SI SIL
RDI EDI DI DIL
RBP EBP BP BPL
RSP ESP SP SPL
R8 R8D R8W R8B
R9 R9D R9W R9B
R10 R10D R10W R10B
R11 R11D R11W R11B
R12 R12D R12W R12B 8 New registers
R13 R13D R13W R13B
R14 R14D R14W R14B
R15 R15D R15W R15B
2. Register architecture
Summary: Family of processors
• x86 alone refers to a family of processors and the instruction set they all use.
• x86-32 refer to 32 bit processors compatible with x86 architecture.
• x86-16 refer to 16 bit processors compatible with x86 architecture.
• X86-64 refer to 64 bit processor compatible with x86 architecture(later shortened to x64)
Revision questions
a) List any three general purpose registers and their functions.
b) Differentiate between a special and general purpose register
c) Briefly explain the following concepts as used in CPU registers.
i. Loading register
ii. Register transfer language
iii. Register transfer
3. Assembly languages
• Overview
• Types of assembly languages based on processor architecture
• Comparisons of assembly language types
• Revision questions
3. Assembly languages
Overview
• Every high-level language program must be compiled into assembly language before it can be linked into an executable
program.
• For the high level language programmer, understanding how the compiler generates the assembly language code can
be a great benefit, both for directly writing routines in assembly language and for understanding how the high-level
language routines are converted to assembly language by the compiler.
• The first step in learning assembly language programming is defining just what type of assembly language
programming you want to (or need to) use in your environment.
• Opcodes: Short for operation codes is part of the machine code that specifies the type of operation to be performed
by the processor.
Types of assembly languages
• Assembly language is closely tied to processor architecture. For this reason, we classify assembly languages into four
main types:
i. CISC: Complex Instruction set Computer
ii. RISC: Reduced Instruction-Set Computer
iii. DSP: Digital Signal Processor
iv. VLIW: Very Long Instruction Word
5/19/2019 Kenga©2019 Lecture notes 20
BSD 1308: Assembly language programming
3. Assembly languages
a) CISC Assembly language: Language for CISC processors. It was developed when programmers wrote assembly
language. It is complicated, often specialized instructions with many effects. Many, complicated addressing
modes. Examples Intel x86, 68000, PDP-11.
b) RISC Assembly language: Language for RISC processors. Developed in response to growing use of compilers.
Easier-to-target, uniform instruction sets. They make the most common operations as fast as possible. Designed
to be pipelined(storing and executing instructions in an orderly process). Examples SPARC, MIPS, HP-PA,
PowerPC.
c) DSP Assembly language: Language for Digital Signal Processors designed specifically for signal processing
algorithms. Has a lot of regular arithmetic on vectors. Often written by hand. Irregular architectures to save
power, area Substantial instruction-level parallelism. Examples: TI 320, Motorola 56000, Analog Devices.
d) VLIW Assembly Language: Very Long Instruction World assembly was a response to growing desire for
instruction-level parallelism(simultaneous execution of instructions). Using more transistors cheaper than
running them faster. Use of many parallel ALUs. The objective is to keep them all busy all the time. The
processors are heavily pipelined. There is use of more regular instruction set. However, its very difficult to
program by hand. Looks like parallel RISC instructions. Examples Itanium, TI 320C6000
3. Assembly languages
Comparison of the various types of assembly languages
3. Assembly languages
Revision questions.
a) Define assembly language.
b) Describe the following types of assembly languages.
i. CISC Assembly language
ii. RISC Assembly language
c) Briefly explain any two advantages and two disadvantages of assembly languages.
d) Differentiate between high level and low level programming languages.
5. Addressing modes
Lesson outline:
• Overview
• Definition
• Address modes
• Revision questions
5. Addressing modes
Overview
• Recall that an instruction in assembly language consists of two parts: the opcode and operand.
• The opcode specifies what to do with the data.
• The operand specifies where to get the data and put the results.
Example: ADD EAX,EBX ; EAX and EBX are operands ADD is the opcode.
• Address: This is a location in memory of specified data.
Addressing modes
• A specification for generating an address of an operand at run time. It refer to the way in which the operands are
specified.
• The CISC processors do almost all CPU operations (like add, shift, ml e.t.c) using both CPU registers and
memory. Thus addressing modes are critical part of compiling almost all of the instructions.
• There are seven(7) addressing modes where the operand is specified directly or specified in register/memory.
i. Immediate
ii. Direct
iii. Register
iv. Register indirect
v. Register relative
vi. Based indexed
vii. Relative based indexed
5/19/2019 Kenga©2019 Lecture notes 35
BSD 1308: Assembly language programming
5. Addressing modes
• Immediate addressing mode: One of the operands is mentioned directly. The data is available as part of the
instruction. The data is 8 or 16 bit long. There is no memory reference required to fetch data. Example: ADD
EAX,0525H.
• Register addressing mode: In this mode, both the operands are in the register. There is no memory access. Used in
limited number of registers. However very small address field is required to address the register. Generally, this mode
has shorter instructions. Example: MOV EAX,EBX.
• Direct addressing mode: In this mode, one or both operands are in the memory. The operand in the memory is
specified through its effective address. Data in that location will be used for specified operation. If the instruction
contain stack operation, segment register will be SS. Example ADD EAX,[0301]
• Register indirect mode: In this mode, EA(effective address) of operand(s) is/are specified in the register. The physical
address is calculated using segment register and EA. Effective address (EA) is a term used to describe the address of an
operand that is stored in the memory. Data in the physical address is an operand. Example MOV EAX,[EBX].
• Register relative addressing mode: In this mode the effective address(EA) is calculated with reference to instruction
pointer. Effective address=[base/pointer register+8 or 16 bit displacement. The displacement can be a number added
to the register within the [ ]. The base/pointer register are: BX or BP or SI or DI. Example MOV EAX,[EBP+10].
• Based indexed addressing mode: In this mode, the operand’s offset is sum of the content of a base register BX or BP
and an index register ESI or EDI. Example ADD EAX, [EBX+ESI]
• Relative Based indexed addressing mode: In this mode, the address of the operand is calculated as the sum of base
register, index register and 8 bit or 16 bit displacement. Example MOV ECX,[EBX+EDI+20]
5/19/2019 Kenga©2019 Lecture notes 36
BSD 1308: Assembly language programming
5. Addressing modes
Revision questions:
a) Define the following concepts as used in addressing modes.
i. Opcode
ii. Operand
iii. Effective address(EA)
iv. Address mode
b) Using examples describe the following address modes
i. Direct address mode
ii. Register address mode
iii. Register indirect address mode
c) For each of the following assembly statements, state and explain the address modes used.
i. ADD EAX,[0301]
ii. MOV ECX,[EBX+EDI+20]
iii. MOV EAX,04
Registers: AH, AL ,AX ,BH ,BL, BP ,BX ,CH ,CL ,EAX,EBX,ECX etc.
Directives END, ENDIF, ENDM, ENDP, ENDS, EQ ,EQU, EVEN, EXITM, EXTERN,%DEFINE etc.
g) Integer expressions: An integer expression is a mathematical expression involving integer value and arithmetic
operators e.g. 5+2, 34%3 etc.
h) Real number constants: These are values of a continuous quantity represented using the syntax: [sign]
integer.[integer][exponent] e.g. +3.5E3,2.3,-1.2E6 etc.
i) Character constants: These are values represented by enclosing a character in single or double quotes e.g. “A”
or ‘A’.
5/19/2019 Kenga©2019 Lecture notes 41
BSD 1308: Assembly language programming
8. Arithmetic Instructions
Lesson outline
• Overview
• Data transfer instructions
• Basic arithmetic operations
• Revision questions
8. Arithmetic Instructions
Overview
• We recall that a program is a set of step-by-step instructions that directs the computer to do the
tasks.
• Instruction: Is a task to be carried out by the processor at run time.
• Can also be defined as a statement that becomes executable when a program is assembled. Note
instructions are in machine language.
• Instructions are considered to be essential elements in assembly language.
• In this lesson, we will look at two types of instructions: arithmetic and data transfer instructions.
• Generally, the instruction consist of two parts: the mnemonic part and operand part.
• Mnemonic part: Refer to abbreviations of the name of operation being executed
• Operand part: Refer specifies the data being processed by instructions.
• Examples:
i. MOV EAX,6 ; Data transfer instruction
ii. ADD EAX,EBX; Arithmetic instruction
8. Arithmetic Instructions
Data transfer instructions
These instructions are used to transfer the data from the source operand to the destination operand.
These are also known as copy instructions.
• MOV: Used to copy the byte or word from the provided source to the provided destination.
• PUSH: Used to put a word at the top of the stack.
• XCHG: Used to exchange the data from two locations.
Example
Instruction Example Meaning
MOV MOV EAX,5 5 is copied to eax
PUSH PUSH EAX Value in eax is put on top of stack
XCHG XCHG EAX,EBX Values in eax and ebx are interchanged
8. Arithmetic Instructions
Basic arithmetic operations
• The binary arithmetic instructions perform basic integer computations on operands in memory or the general-purpose
registers.
• Some of the common arithmetic mnemonics are: INC,DEC,SUB,ADD,MUL,DIV etc:
• INC: This instruction is used for incrementing an operand by one. It works on a single operand that can be either in a register
or in memory. Syntax: INC destination.
• DEC: This instruction is used for decrementing an operand by one. It works on a single operand that can be either in a register
or in memory. Syntax: DEC destination.
• SUB: This instruction is used for performing simple subtraction of binary data in byte, word and doubleword size, i.e., for
subtracting 8-bit, 16-bit or 32-bit operands. Syntax: SUB destination, source
• ADD: This instruction is used for performing simple addition of binary data in byte, word and doubleword size, i.e., for adding
8-bit, 16-bit or 32-bit operands. Syntax: ADD destination, source.
• MUL/IMUL: There are two instructions for multiplying binary data. The MUL (Multiply) instruction handles unsigned data and
the IMUL (Integer Multiply) handles signed data. Both instructions affect the Carry and Overflow flag. Syntax: MUL/IMUL
multiplier.
• DIV/IDIV: The division operation generates two elements - a quotient and a remainder. In case of multiplication, overflow
does not occur because double-length registers are used to keep the product. However, in case of division, overflow may
occur. The processor generates an interrupt if overflow occurs. The DIV (Divide) instruction is used for unsigned data and the
IDIV (Integer Divide) is used for signed data. Syntax: DIV/IDIV divisor
5/19/2019 Kenga©2019 Lecture notes 74
BSD 1308: Assembly language programming
8. Arithmetic Instructions
The table below summarize the common arithmetic operations
Type X86 mnemonics Examples C/C++ equivalent
Immediate imul imul ebx,11h ebx*=0x11
xor xor di,55h di^=0x55
add add esi,8 esi+=8
Register inc inc ecx ecx+1
add add ebx,esi ebx+=esi
mul mul ebx edx: eax=eax*ebx
Memory add add eax,[val1] eax+=*val1
or or ecx,[ebx,esi] ecx|=*(ebx+esi)
sub sub word ptr[edi],12 *(short*)edi-=12
8. Arithmetic Instructions
Example: Assembly language program that uses imul, add,sub and incr mnemonics.
8. Arithmetic Instructions
Revision questions
a) Briefly describe the following arithmetic instructions:
i. SUBB
ii. IDV
iii. MUL
iv. IMUL
b) Write an assembly language program to add FFFFh and 0001h and store the sum
in an accumulator.
c) Explain the contents of MUL operands.
d) Write an assembly language program to multiply any two 8-bit numbers
9. Control structures
Lesson outline:
• Overview
• Definitions
• Types of control structures
• Branch instructions
• Jumps
• Flags
• Branch on condition
• Revision questions
9. Control structures
Overview
• Usually the instructions in a program are executed in order, one after another. Often, though, we want to make
decisions.
• A control structure is a block of code that analyses variables and chooses a direction in which to go based on
given parameters.
• The flow control determines how the CPU will respond when given certain conditions and parameters.
Basic terms:
• Preconditions: The state of variables before entering a control structure.
• Post conditions: The state of variables after the execution of the program.
Types of control structures
a) Sequence structure: Default structure.
b) Selection structure: Conditional execution
c) Repetition structure: Loops.
NB: The repetition and selection structures involve branching.
9. Control structures
Sequence structure: Statements are executed one after another from top to bottom.
9. Control structures
Branch instructions
• In assembly programming, branch instruction is used to implement the repetition and selection
control structures.
• A branch is an instruction in a program that can cause the CPU to begin executing a different
instruction sequence.
• Branch can also refer to a break in the sequential flow of instructions that the processor is executing.
• Also refer to the act of switching execution to a different instruction sequence as a result of
executing a branch instruction.
• Branching in assembly, is a technique that allows certain piece of the code to be executed based on a
condition or to be executed many times.
• In assembly, branching is done using two types of instructions: compare instruction and conditional
jump instruction.
• Example of compare instruction cmp.
• Examples of conditional jump instructions je, jg, jna e.t.c.
5/19/2019 Kenga©2019 Lecture notes 81
BSD 1308: Assembly language programming
9. Control structures
Jumps
• Jumps are useful when implementing branching in assembly language.
• A jump is an instruction in a computer program that causes processing to move to a different place in
the program sequence.
• Conditional execution or jumps can be classified into two broad categories.
a) Unconditional jump: The code needs to jump to another location regardless. This is performed by
the JMP instruction. The JMP instruction transfers control unconditionally to another instruction:
syntax:
JMP label
b) Conditional jump: This occur when some condition being met such as a flag being 1 or 0. This is
performed by a set of jump instructions j<condition> depending upon the condition:
Syntax:
cmp operand1,operand2
JE Label
5/19/2019 Kenga©2019 Lecture notes 82
BSD 1308: Assembly language programming
9. Control structures
Example 1: A program to show unconditional jump in assembly. Similar to break in high level language.
9. Control structures
Example 2: A program to show conditional jump in assembly. Similar to IF… ELSE in high level language.
9. Control structures
Flags register
• Flags register: This is a modified kind of register that record the condition of a microprocessor's
calculation.
• A flag(CPU Flag) is a value that acts as a signal for a function or process.
• The value of the flag is used to determine the next step of a program.
• Common types of the flags include:
i. Carry flag: Indicate that an instruction generates a result that is too large (or too small) for the
destination operand when viewed as an unsigned integer
ii. Sign flag: Indicates whether a calculation was positive or negative.
iii. Overflow flag: Indicates when the result of a process exceeds a set word limit.
iv. Parity flag: Indicates that a process has produced an even number of "1"s.
v. Zero flag: Indicates that the result of an operation equals zero.
vi. Parity flag: Indicates that an instruction generates an even number of 1’s bits in the low byte
of the destination operand.
5/19/2019 Kenga©2019 Lecture notes 85
BSD 1308: Assembly language programming
9. Control structures
Branch on condition:
• A programming instruction that directs the computer to another part of the program based on the results of a compare.
• The most common way to transfer control in assembly language is to use a conditional jump. This is a two-step process:
1. First test the condition.
2. Then jump if the condition is true or continue if it is false.
• Conditional jump instructions can be divided into four groups:
i. Jumps based on the value of a single arithmetic flag
ii. Jumps based on the value of CX or ECX
iii. Jumps based on comparisons of signed operands
iv. Jumps based on comparisons of unsigned operands.
• Conditional Jump Instruction has the following syntax:
cmp operand1,operand2; comparison
Jcond destination ; cond is the jump condition
Example 1: Compare unsigned EAX to EBX, and copy the larger of the two into a ECX.
Solution: See next slide
9. Control structures
Branch on condition:
Example 1: Compare unsigned EAX to EBX, and copy the larger of the two into a ECX.
9. Control structures
Jumps: The following is a list of jumps based on the Zero, Carry, Overflow, Sign, and Parity flags.
9. Control structures
Jumps: The following table shows a list of signed jumps based on comparison of signed
operands
9. Control structures
Jumps: The following shows a list of unsigned jumps based on comparisons of unsigned operands.
9. Control structures
Selection structure: A statement is executed based on a condition. Recall if..else?
9. Control structures
Repetition structure: The power of CPUs is in their ability to repeat actions and to alter their operation depending
on data. Repeat action/iteration occur when statement(s) are executed repeatedly until some condition becomes
false.
9. Control structures
Example 1: Program to print series of numbers in descending order.
9. Control structures
Example 2: Program to print series of odd numbers in ascending order.
9. Control structures
Revision questions.
a) Define the term jump as used in assembly programming branching
b) Describe the types of jumps in assembly programming.
c) Write code snippets in assembly language for each of the following cases.
i. Jump to a label if an integer is even.
ii. jumps to a label if an integer is negative.
iii. jump to a label if either bit 0 or bit 1 in EAX is set.
iv. Jump to a label if signed EAX is greater than EBX
v. Compare unsigned EAX to EBX, and copy the larger of the two into a variable named Large
vi. Jump to label L1 if the memory word pointed to by ESI equals Zero
vii. Jump to label L2 if the doubleword in memory pointed to by EDI is even
12. Arrays
Lesson outline:
• Overview
• Array declarations
• Accessing array elements
• Using arrays in assembly programs
• Advantages of arrays
• Disadvantages of arrays
• Revision questions
12. Arrays
Overview:
• An array is defined as a collection of data elements, all of the same type, that are stored in
contiguous memory locations.
• The address of the array variable is called the base address of the array.
• The address of an array element may be computed by adding a constant to the base address
• Arrays can be one , two or more dimensions.
Array declaration and initialization
• In assembly language arrays can be declared in two ways:
a) Initialized Lists :An initialized array is defined in the same way as a scalar variable, but with multiple
initial values:
Example: SCORES DW 45,56,54,34,43,54,51
b) Using TIMES directive: The TIMES directive is used for multiple initializations to the same value.
Example: SCORE TIMES 7 DW TIMES 0
12. Arrays
Using arrays
• To access an array in assembly language, we use a pointer.
• Pointer: A pointer is simply a register or variable that contains a memory address.
Accessing members:
Location of any array member is calculated by knowing the following:
a) Address of the first member of the array.
b) The size in bits in each member of the array.
c) Index of the desired member.
Initialization
How to initialize
• Array1 db 1,2,3,4
• Array2 db ‘a’,’b’,’c’
• Array3 db ‘abc’
5/19/2019 Kenga©2019 Lecture notes 113
BSD 1308: Assembly language programming
12. Arrays
Advantages of arrays.
• Several elements of same data type can be accessed using single name.
• Can be useful when implementing data structures such as stacks.
• Arrays are easy to create.
Disadvantages of arrays
• The size of the array must be known in advance.
• Array is static structure: it has a fixed size.
• Memory wastage can result if we allocate more memory than required.
12. Arrays
Example: A program to store 8 numbers in an array, then add and display their sum.
12. Arrays
Example: A program to store 11 numbers in an array, then display their values.
12. Arrays
Revision questions:
a) Differentiate between array and variable.
b) Write an assembly language program to find the smallest number in an array.
c) Write an assembly language program to sort a given array of numbers in an ascending order.
d) Explain the importance of an array in assembly language programming
13. Procedures
Lesson outline:
• Overview
• Syntax of a procedure
• Calling a procedure
• Returning from procedure
• Uses of procedures
• Revision questions
13. Procedures
Definition
• It is a named set of commands which can be executed in order.
• A collection of instructions to which we can direct the flow of the program and once the execution of
the instructions is completed, the control is given back to the next line to the process of the code
that called the procedure.
• A block of code that can be called anywhere in the program with name.
• Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size.
• Call: This instruction pushes the return address onto the stack and transfers control to a procedure.
• Ret: This instruction pops the return address off the stack and returns control to that location.
Syntax:
Procedure_name
Body
Return statement
• Procedures are identified by a name.
• The body of the procedure describes what the procedure does.
• End of the procedure is indicated by a return statement.
5/19/2019 Kenga©2019 Lecture notes 119
BSD 1308: Assembly language programming
13. Procedures
Procedure calling:
• The procedure is called from another function by using the CALL instruction.
• The CALL instruction should have the name of the called procedure as an argument.
• Syntax: CALL PROCEDURE_NAME
• Basically the calling instruction may call a procedure and pass:
• Nothing
• Values
Returning from procedure:
13. Procedures
Consider the procedures below.
13. Procedures
Advantages of procedures
• Decomposes a complex programming task into simpler steps.
• Reduces code duplication
• Improves readability of the code.
• Dividing a large programming task among various programmers, or various stages of a project
Disadvantages of procedures
• There is computation overhead in the call mechanism.
• Debugging and verification is difficult.
• More development time is required.
• Extra code may be required to integrate the procedures.
13. Procedures
Example: A program that uses two procedures, that prints some text/string.
13. Procedures
Passing arguments to a procedure:
• When writing procedures it is usually necessary to pass information to the procedure to customize its
operation. There are two possible ways to achieve this:
i.Store the information(arguments) in various registers.
ii.Store the arguments in the stack.
• In this lesson, we will use stacks.
• Before calling a procedure, all its parameters must be stored on the stack.
push eax
push ebx
call SomeProcedure
• The procedure can now access these variables/arguments on the stack. Recall that the stack pointer (SP /ESP-
register) points to the topmost element on the stack.
• The code for the procedure will therefore look like:
SomeProcedure:
push ebp
mov ebp,esp
...
mov esp,ebp
pop ebp
ret
5/19/2019 Kenga©2019 Lecture notes 124
BSD 1308: Assembly language programming
13. Procedures
Passing arguments to a procedure:
• Save the EBP register at the top of the procedure code . The first MOV statement sets up the EBP register with
the initial value of the ESP register. The last statement in the procedure will restore the stack if it wasn't cleared
up properly within the procedural code. The EBP register is then restored.
• After calling the procedure, it is also necessary to clear the parameters off the stack. This is done by the ret
instruction after returning to the calling code.
Example 1: Sum of two numbers calculated using function
13. Procedures
Example 2: Write an assembly program using NASM to receive two integers then pass their values to a function
called Adding that calculates the sum and returns the appropriate answer.
13. Procedures
Example 3: Write an assembly program using NASM to receive two integers then pass their values to a function
called Adding that calculates the sum and returns the appropriate answer.
13. Procedures
Example 4: Write an assembly program using NASM to receive two integers then pass their values to a function
called multiply that calculates the product and returns the appropriate answer.
13. Procedures
Uses of procedures
• Organization: Used in to organize the program in order to reduce complexity.
• Reusability: The same procedure can be called many times within the program.
• Size of the code: Procedures help to reduce code redundancy .
• Debugging: Easier to debug the program because size of the code is greatly reduced.
• Abstraction: We only need to call the name of the procedure for it to execute the task.
13. Procedures
Revision questions:
a) Define the following concepts with regard to procedures in assembly language programming
i. Procedure
ii. Call instruction
iii. Ret instruction
b) Describe the importance of a procedure in assembly language programming.
c) Write a procedure named multiply that computes the product of two signed 32-bit operands. The
operands will be passed in registers ESI and EDI. The procedure should return the result on EAX.
Write a program that uses the multiply procedure.
d) Write a procedure named multiply that computes the product of three unsigned 32-bit operands
stored in variables x, y and z. The operands will be passed in registers EAX,EBX and ECX. The
procedure should return the result on EAX. Write a program that uses the multiply procedure .
e) Write Assembly Language code to evaluate expression: ((a+b)-(c+d))-e. Assume a,b,c,d and e are
integer variablesed2.Hint: Use procedure.
14. References
• Assembly Language Programming for Intel Processors Family by Vasile Lungu.
• An introduction to assembly language programming and computer architecture by Joe Carthy.
• Microprocessors and interfacing : programming and hardware by Douglas V. Hall.
• Kusswurm, D. (2014). Modern X86 Assembly Language Programming: 32-bit, 64-bit, SSE, and AVX, 700.
https://doi.org/10.1007/978-1-4842-0064-3
• https://www.tutorialspoint.com/assembly_programming/
• http://www.pravaraengg.org.in/Download/MA/assembly_tutorial.pdf
• http://www1.cs.columbia.edu/~sedwards/classes/2002/w4995-02/assembly.9up.pdf
• http://www.philadelphia.edu.jo/academics/qhamarsheh/uploads/Lecture%2018%20Conditional%20Jumps%20Instructions.
pdf
• http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
• https://www.csie.ntu.edu.tw/~cyy/courses/assembly/08fall/lectures/handouts/lec11_x86Asm_4up.pdf
• http://www.plantation-productions.com/Webster/www.artofasm.com/DOS/ch09/CH09-7.html
• http://cs.lmu.edu/~ray/notes/x86assembly/
• http://www.eecg.toronto.edu/~amza/www.mindsec.com/files/x86regs.html
• https://www.assemblylanguagetuts.com/x86-assembly-registers-explained/
• http://www.sciencehq.com/computing-technology/cpu-registers.html
• http://libra.cs.virginia.edu/~aaron/08-nasm/nasmexamples.html
• http://site.iugaza.edu.ps/ahaniya/files/Assembly-Language-Lab9.pdf
• https://www.cs.bgu.ac.il/~caspl162/wiki.files/PS02_162[1].pdf
• http://www.husseinsspace.com/teaching/udw/1996/asmnotes/chapfive.htm
5/19/2019 Kenga©2019 Lecture notes 131
BSD 1308: Assembly language programming
THANK YOU