100% found this document useful (2 votes)
122 views

Assembly Language

Notes on Assembly language

Uploaded by

Allan Wafula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
122 views

Assembly Language

Notes on Assembly language

Uploaded by

Allan Wafula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 132

BSD 1308: Assembly language programming

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]

5/19/2019 Kenga©2019 Lecture notes 1


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 3


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 7


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 8


BSD 1308: Assembly language programming

2. Register architecture
Lesson outline:
• Overview
• Types of CPU registers
• Segmented memory model
• Advantages of registers
• Disadvantages of registers
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 9


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 10


BSD 1308: Assembly language programming

2. Register architecture

The central processing unit or processor


or microprocessor consists of four main
components:
1. Control component- The control unit.
2. Transfer component-The bus
3. Processing component- The ALU
4. Storage component-The registers
and Flags
…………………………………………………………………
Remark: We will use x86 processors. The
x86 family of CPUs contains 16, 32, and 64-
bit processors from several manufacturers,
with backward-compatible instruction sets,
going back to the Intel 8086 introduced in
1978.

32-bit register architecture


5/19/2019 Kenga©2019 Lecture notes 11
BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 12


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 13


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 14


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 15


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 16


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 17


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 18


BSD 1308: Assembly language programming

3. Assembly languages
• Overview
• Types of assembly languages based on processor architecture
• Comparisons of assembly language types
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 19


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 21


BSD 1308: Assembly language programming

3. Assembly languages
Comparison of the various types of assembly languages

CISC RISC DSP VLIW


Opcodes Many ,complex Few ,simple Few , complex Few ,simple
Registers Few, sometimes Many, general Few, sometimes Many, general
special purpose special purpose
Addressing modes Many Few Special Few
Instruction level parallelism None None Restricted plenty

5/19/2019 Kenga©2019 Lecture notes 22


BSD 1308: Assembly language programming

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/19/2019 Kenga©2019 Lecture notes 23


BSD 1308: Assembly language programming

4. Assembly language programming tools


Lesson outline:
• Overview
• Assemblers
• Linkers
• Debuggers
• Editors
• IDEs
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 24


BSD 1308: Assembly language programming

4. Assembly language programming tools :


Overview
• These are software tools are used for editing, assembling, linking, and debugging assembly
language program.
• Also defined as a computer program that software developers use to create, debug, maintain, or
otherwise support other programs and applications.
• In this lesson we look at assembler, a linker, a debugger, and an editor.

5/19/2019 Kenga©2019 Lecture notes 25


BSD 1308: Assembly language programming

4. Assembly language programming tools :


a) Assembler
• It is a utility program that interprets software programs written in assembly language into machine language,
code and instructions that can be executed by a computer.
• An assembler primarily serves as the bridge between symbolically coded instructions written in assembly
language and the computer processor memory and other computational components.
• An assembler works by assembling and converting the source code of assembly language into object code or
an object file that constitutes a stream of zeros and ones of machine code, which are directly executable by
the processor.

5/19/2019 Kenga©2019 Lecture notes 26


BSD 1308: Assembly language programming

4. Assembly language programming tools :


a) Assembler
Functions of assemblers
• There are five key functions of assemblers:
i. Convert mnemonic operation codes to machine language equivalents.
ii. Convert symbolic operands to machine addresses
iii. Build machine instructions in the proper format.
iv. Convert data constants to internal representations
v. Write the object program and assembly listing files
Types of assemblers
• There are two types of assemblers based on how many passes through the source are needed to produce the executable
program:
i. One-pass assemblers: They go through the source code once and assume that all symbols will be defined before any
instruction that references them. The advantage of a one-pass assembler is speed, which is not as important as it once
was with advances in computer speed and abilities.
ii. Two-pass assemblers: These assemblers create a table with all symbols and their values in the first pass, then use
the table in a second pass to generate code. The assembler must at least be able to determine the length of each
instruction on the first pass so that the addresses of symbols can be calculated. The advantage of the two-pass
assembler is that symbols can be defined anywhere in program source code. NASM is a 2-pass assembler.
5/19/2019 Kenga©2019 Lecture notes 27
BSD 1308: Assembly language programming

4. Assembly language programming tools :


b). Linkers.
• A software tool that takes one or more object files generated by a assembler and combines
them into one, executable program.
• It combines the program's object file created by the assembler with other object files and
link libraries, and produces a single executable program.
• The assembler typically generates one object file. Thus one source code file will give one
object file(see diagram in the next slide).
• Libraries are also object files.
• All these files might have been compiled by separate assemblers. The major task of a linker
is to search and locate referenced module/routines in a program and to determine the
memory location where these codes will be loaded, making the program instruction to have
absolute references.
• In summary, the overall job of the linker is to put together the various areas in all the object
files, getting an executable file that is ready to load into memory and run.
5/19/2019 Kenga©2019 Lecture notes 28
BSD 1308: Assembly language programming

4. Assembly language programming tools :


b). Linkers.

5/19/2019 Kenga©2019 Lecture notes 29


BSD 1308: Assembly language programming

4. Assembly language programming tools :


c). Debuggers
• Debugger is a software tool, which allows us to stop the executing program and examine or change
its state.
• It allows the programmer to load object code program into system memory, execute the program
and troubleshoot or debug it.
• Debugging is the process of finding the clues and interpreting these clues to find the problem.
• Debugging helps in finding and fixing the bugs.
• Debugging will involve use of program trace and breakpoints.
• Program trace: That involves stepping through the program one statement at a time. Values of
registers are shown during each step.
• Breakpoint: They cause program flow to be interrupted, and control transferred to the debugger
(the user). The program is run to the end, unless the breakpoints have been defined.

5/19/2019 Kenga©2019 Lecture notes 30


BSD 1308: Assembly language programming

4. Assembly language programming tools :


d). Editors
• These are software tools that allows you to create and edit assembly language source files
• Some editors provide syntax highlighting features and can be customized as a programming
environment.
• Examples of editors used Notepad, Norton’s editor, word star, Sublime text, Notepad++, Emacs, Jed
etc.
• The code created using text editors consist of plain text(Plain text exclusively consists of character
representation).
• Each character is represented by a fixed-length sequence of one, two, or four bytes, or as a
variable-length sequence of one to four bytes, in accordance to specific character
encoding conventions, such as ASCII, UTF-8, or Unicode.
• Some text editors are proprietary while others are open source.
• Some common features include spell checkers, cut/copy, paste, find, replace, block selection etc.
• Always remember to save the source code file using appropriate extension e.g. asm.

5/19/2019 Kenga©2019 Lecture notes 31


BSD 1308: Assembly language programming

4. Assembly language programming tools :


e). Assembly language IDEs.
An integrated development environment(IDEs) is a programming tool that provides interfaces for users to write
code. In particular an IDE will provide text editors, debuggers, compilers/assemblers, loaders etc. In this course we
will be using SASM as the IDE.
a) SASM: simple Open Source cross platform IDE for NASM, MASM, GAS, FASM assembly languages. SASM
has syntax highlighting and debugger.
b) NASM: The Net wide Assembler (NASM) is a popular assembler for the x86/x64 architecture. NASM is
open source software and is freely available . It is available for many operating systems, including (but not
limited to) windows.
c) GAS: The GNU Assembler is a collection of assemblers - one for each of the platforms supported by GCC.
GAS provides a very solid and well-supported assembler that is fully integrated with the other GNU tools
(implicit make rules, inline assembly from C/C++ source).
d) FASM: Flat assembler is an assembler for x86 processors. It supports Intel-style assembly language on
the IA-32 and x86-64 computer architectures. Has high speed, size optimizations, operating system (OS)
portability, and macro abilities.
e) MASM: Microsoft assembler provides much flexibility when it comes to development on windows
environment over various other assemblers.
5/19/2019 Kenga©2019 Lecture notes 32
BSD 1308: Assembly language programming

4. Assembly language programming tools :


Revision questions.
a) Briefly describe the following assembly language programming tools.
i. Linkers
ii. Editors
iii. Assembler
b) Differentiate between one-pass and two-pass assemblers.
c) Describe any THREE functions of an assembler.
d) Explain any THREE benefits of using IDEs where creating a source code.
e) Discuss the following Assembly language IDEs.
i. NASM
ii. FASM
iii. MASM

5/19/2019 Kenga©2019 Lecture notes 33


BSD 1308: Assembly language programming

5. Addressing modes
Lesson outline:
• Overview
• Definition
• Address modes
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 34


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 37


BSD 1308: Assembly language programming

6. Assembly language elements


Lesson outline:
• Overview
• Lexical elements
• Assembly operators
• Variable declarations
• Constant declarations
• Assembly program errors
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 38


BSD 1308: Assembly language programming

6. Assembly language elements


Overview:
Assembly language is a language that microcontroller and man use to communicate. The assembly language represents a
set of rules used in writing a program for a microcontroller, and the assembler is a program on the personal computer
which translates assembly language into a language of zeros and ones.

Lexical elements of assembly language


The lexical elements of a language are the words, punctuation, and grammar rules of the language. Assembly language has
the following lexical elements:
a) Statements
b) Integer constants
c) Integer expressions
d) Character and string constants
e) Reserved words and identifiers
f) Directives and instructions
g) Labels
h) Mnemonics and Operands
i) Comments
5/19/2019 Kenga©2019 Lecture notes 39
BSD 1308: Assembly language programming

6. Assembly language elements


a) Statements: A complete instruction to the computer to perform some task e.g. MOV EAX,
EBX. Note statements are in assembly language.
b) Comments: Brief explanation about the statement. Start with a semicolon (;)
c) Instructions: A task to be carried out by the processor at run time. An instruction is a
statement that becomes executable when a program is assembled. Note instructions are in
machine language.
d) Directives: Are instructions used by the assembler to help automate the assembly process
and to improve program readability e.g. %define, %include etc.
e) Reserved words: These are keywords with special meaning. Should never be used as
identifiers. Group Keywords

Operands and symbols $, DWORD ,FAR ,NEAR ,REAL4 etc.

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.

Processor instructions POP,PUSH, MOV,MOVS,LEA, LOOP,OUT etc.

5/19/2019 Kenga©2019 Lecture notes 40


BSD 1308: Assembly language programming

6. Assembly language elements


f). Integer constants: These are numerical values represented using the syntax:
[+/-]digits[radix] e.g. -056b,+67h etc.
Radix character Type examples
h hexadecimal -034h,045h
d Decimal -45d,+34d,32d,56
o Octal 5o
b binary 10b,1101b
r Encoded real 34r

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

6. Assembly language elements


j) String constants: These are values enclosed by single or double quotes e.g. “hello”.
k). Labels: This is a symbol that represents the memory address of an instruction or data.
Usually placed at the beginning of a statement. Ends with a semi colon. There are two types of
labels- symbolic label(identifier followed by :) and numeric label(number(s) followed by :).
l). Mnemonics: Mnemonics are a set of readily memorized programming instructions that are
later translated into pure machine code by assembler.
m) opcode: Short for operation code. It tells the processor what operation should be
performed.
n) Operands: These are are the "arguments" or "parameters" that directly follow a mnemonic.
o). Identifiers: These constitute of names used in assembly language formed from letters,
digits and special characters –Special characters.

5/19/2019 Kenga©2019 Lecture notes 42


BSD 1308: Assembly language programming

6. Assembly language elements


o). Identifiers: These constitute of names used in assembly language formed from letters,
digits and special characters –Special characters. Creating identifiers in assembly language.
p). Variables: Named memory locations. To create a variable use directives.
[variable_name] directive [initial values]
x DB 3h //Declare variable x whose initial value is 3h
y: times 4 DB 0 ;//Declare variable y of 4 bytes of initial value 0
Directive Purpose Storage Space
DB Define Byte allocates 1 byte
DW Define Word allocates 2 bytes
DD Define Doubleword allocates 4 bytes
DQ Define Quadword allocates 8 bytes
DT Define Ten Bytes allocates 10 bytes

5/19/2019 Kenga©2019 Lecture notes 43


BSD 1308: Assembly language programming

6. Assembly language elements


Constants
• Named memory location that store some value.
• String constants can be created using the keyword DB and DD.
• Floating point constants can be created using keywords DW,DQ,DD and DT.
• Numeric constants can be decimal, binary, octal or hexadecimals
• Character constants consists of character in single or double quotes.

5/19/2019 Kenga©2019 Lecture notes 44


BSD 1308: Assembly language programming

6. Assembly language elements


Operators
The assembler supports the following operators for use in expressions
Type Operator Name Example Result?
Arithmetic operators + Addition MOV EAX, (4+3) 7
- Subtraction MOV EAX, (8-5) 3
* Multiplication MOV EAX,(6*4) 24
/ Division MOV EAX,(7/2) 3
Relational operators CMP Compare CMP EAX,EBX Used with jumps
Assignment operators MOV Move/Assign MOV EAX,7 7
Logical operators NOT Logical NOT NOT EAX 1’s complement
OR Logical OR OR EAX,EBX Same as |
AND Logical AND AND EAX,EBX Same as &
Bitwise operators & Bitwise logical AND MOV EAX,(7&5) 5
| Bitwise logical OR MOV EAX,(7|5) 7
>> Shift right MOV EAX,(20>>2) 5
<< Shift left MOV EAX,(20<<2) 80
^ Bitwise logical XOR MOV EAX,(20^2) 22

5/19/2019 Kenga©2019 Lecture notes 45


BSD 1308: Assembly language programming

6. Assembly language elements


Assembly program errors
• Programming errors are called bugs and the process of tracking them down and correcting
them is called debugging.
• Some errors can occur when building the assembly code e.g syntax errors.
• Other errors occur when the assembly program is executing e.g. semantic errors.
• Generally we look at three types of errors:
a) Syntax errors: Errors that occur when the rules of assembly language are violated e.g.
misspelt mnemonic.
b) Semantic errors: Errors that occur when statements have different meaning from
what was intended e.g. wrong formula.
c) Logic error: Errors that make the program to produce incorrect or undesired output
e.g. divide number by 0.

5/19/2019 Kenga©2019 Lecture notes 46


BSD 1308: Assembly language programming

6. Assembly language elements


Revision questions:
a) Define the concept of lexical elements as used in assembly language.
b) Describe any FIVE elements of a assembly language.
c) State and explain THREE keywords used in assembly language
d) Differentiate between numeric and symbolic labels in assembly language.
e) Differentiate between integer constants and integer expressions in assembly language.

5/19/2019 Kenga©2019 Lecture notes 47


BSD 1308: Assembly language programming

7. Assembly language program structure


Lesson outline
• Overview
• Program sections
• Program statements
• Program directives
• Program syntax
• Assembly language stacks
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 48


BSD 1308: Assembly language programming

7. Assembly language program structure


Overview
All programs in general work as follows:
1. Read data from an input device, such as the keyboard, a disk file, the internet, etc., into main
memory.
2. Load data from main memory into CPU registers.
3. Perform arithmetic/logic operations on the data.
4. Store the results in main memory.
5. Write the results to an output device, such as the screen, a disk file, audio speakers, etc.

5/19/2019 Kenga©2019 Lecture notes 49


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly program sections
An assembly program can be divided into three sections −
• data section
• bss section
• text section.
a) The data section: Used for declaring initialized data or constants. This data does
not change at runtime. The syntax for declaring data section is section.data
b) The bss section: The bss section is used for declaring variables. The syntax for
declaring bss section is section.bss
c) The text section: Used for keeping the actual code. This section must begin with
the declaration global _start or main, which tells the kernel where the program
execution begins.
5/19/2019 Kenga©2019 Lecture notes 50
BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language program
Assembly program to print hello world .

5/19/2019 Kenga©2019 Lecture notes 51


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language program
Assembly program to print hello world .

5/19/2019 Kenga©2019 Lecture notes 52


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language statements.
A statement is an instruction to the processor to accomplish some task.
• Assembly language programs consist of three types of statements −
i. Executable instructions or instructions
ii. Assembler directives or pseudo-ops
iii. Macros.
• The executable instructions or simply instructions tell the processor what to do. Each
instruction consists of an operation code (opcode). Each executable instruction generates
one machine language instruction.
• The assembler directives or pseudo-ops tell the assembler about the various aspects of the
assembly process. These are non-executable and do not generate machine language
instructions.
• Macros: These are mechanisms for generating a commonly used sequence of instructions.
5/19/2019 Kenga©2019 Lecture notes 53
BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language statements.
• Assembly language statements are entered one statement per line. Each statement
follows the following format −
[label] mnemonic [operands] [;comment]
• Label: Symbolic labelling of an assembler address (command address at machine level).
• Mnemonic: Symbolic description(Usually abbreviations) of an operation.
• Operands: Contains variables or addresses if necessary.
• Comments: Brief description of a statement.
NB: The fields in the square brackets are optional.

5/19/2019 Kenga©2019 Lecture notes 54


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language statements.
• The fields in the square brackets are optional.
• A basic instruction has two parts, the first one is the name of the instruction (or the
mnemonic), which is to be executed, and the second are the operands or the
parameters of the command.
• Examples of assembly language statements −
INC COUNT ; Increment the memory variable COUNT
MOV EAX, 48 ; Transfer the value 48 in register EAX.
ADD eax,edx ;Add contents of edx into eax

5/19/2019 Kenga©2019 Lecture notes 55


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language directives:
• Directives: These are statements that provide information to assembler on various aspects of the
assembly process. they are non-executable -do not generate machine language instructions.
examples.
₋ %include-loads another source file during assembly.
₋ equ- assigns a value to a symbol (same as =)
₋ db – used to declare a byte -2-byte variable.
₋ extern- tells the assembler that the name or label following the directive is from some other
assembly module.
₋ global-used to make the symbol available to the other modules.
₋ proc- it is used to identify the start of a procedure.
₋ dd- used to declare a DWORD – a DWORD double word is made up of 32 bits =2 word’s or 4 byte.

5/19/2019 Kenga©2019 Lecture notes 56


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language program syntax

5/19/2019 Kenga©2019 Lecture notes 57


BSD 1308: Assembly language programming

7. Assembly language program structure


Assembly language syntax.
The syntax of an assembly language program statement obeys the following rules:
• Only one statement is written per line
• Each statement is either an instruction or an assembler directive
• Each instruction has an opcode and possibly one or more operands
• An opcode is known as a mnemonic.
• Each mnemonic represents a single machine instruction
• Operands provide the data to work with.
• Labels are terminated with semi-colon.

5/19/2019 Kenga©2019 Lecture notes 58


BSD 1308: Assembly language programming

7. Assembly language program structure


Stacks in assembly language
• A stack is an array-like data structure in the memory in which data can be stored and removed from a location
called the 'top' of the stack.
• The data that needs to be stored is 'pushed' into the stack and data to be retrieved is 'popped' out from the
stack.
• Stack is a LIFO data structure, i.e., the data stored first is retrieved last.
• Assembly language provides two instructions for stack operations: PUSH and POP. These instructions have
syntaxes like –
• push operand
• pop address/register (or add esp,4)…adding 4 to the stack pointer to
revert back from push changes
• The registers SS and ESP (or SP) are used for implementing the stack.
• The top of the stack, which points to the last data item inserted into the stack is pointed to by the SS:ESP
register, where the SS register points to the beginning of the stack segment and the SP (or ESP) gives the offset
into the stack segment.

5/19/2019 Kenga©2019 Lecture notes 59


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 1: A program to add two whole numbers

5/19/2019 Kenga©2019 Lecture notes 60


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 2: A program to show FIFO use of stacks

5/19/2019 Kenga©2019 Lecture notes 61


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 3: A program to add two real numbers

5/19/2019 Kenga©2019 Lecture notes 62


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 4: A program to divide two real numbers

5/19/2019 Kenga©2019 Lecture notes 63


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 5: A program to add more than two numbers stored in registers: EAX, EBX, ECX,EDX,EDI,ESI and EBP.

5/19/2019 Kenga©2019 Lecture notes 64


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 6: Simple program to demonstrate character, string and number constants.

5/19/2019 Kenga©2019 Lecture notes 65


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 7: Simple program to demonstrate use of integer expressions.

5/19/2019 Kenga©2019 Lecture notes 66


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 8: Simple program to read two integers then display their values (use scanf and printf).

5/19/2019 Kenga©2019 Lecture notes 67


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 9: Simple program to read two integers then display their sum (use scanf and printf).

5/19/2019 Kenga©2019 Lecture notes 68


BSD 1308: Assembly language programming

7. Assembly language program structure


Example 10: Simple program to read two integers then swap and display them (use scanf and printf).

5/19/2019 Kenga©2019 Lecture notes 69


BSD 1308: Assembly language programming

7. Assembly language program structure


Revision questions.
a) Describe the general working of any program.
b) Briefly explain the format of an assembly language program.
c) Discuss the THREE types of assembly language statements.
d) Brief state and explain any THREE assembly language directives
e) Describe the TWO key components of an instruction in assembly language program.
f) Write a simple assembly language program to add two binary numbers 1010b and 1100b.
g) Write a simple assembly language program to multiply two real numbers 3.72 and 1.78 and display
the answer in four decimal places.

5/19/2019 Kenga©2019 Lecture notes 70


BSD 1308: Assembly language programming

8. Arithmetic Instructions
Lesson outline
• Overview
• Data transfer instructions
• Basic arithmetic operations
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 71


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 72


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 73


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 75


BSD 1308: Assembly language programming

8. Arithmetic Instructions
Example: Assembly language program that uses imul, add,sub and incr mnemonics.

5/19/2019 Kenga©2019 Lecture notes 76


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 77


BSD 1308: Assembly language programming

9. Control structures
Lesson outline:
• Overview
• Definitions
• Types of control structures
• Branch instructions
• Jumps
• Flags
• Branch on condition
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 78


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 79


BSD 1308: Assembly language programming

9. Control structures
Sequence structure: Statements are executed one after another from top to bottom.

5/19/2019 Kenga©2019 Lecture notes 80


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 83


BSD 1308: Assembly language programming

9. Control structures
Example 2: A program to show conditional jump in assembly. Similar to IF… ELSE in high level language.

5/19/2019 Kenga©2019 Lecture notes 84


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 86


BSD 1308: Assembly language programming

9. Control structures
Branch on condition:
Example 1: Compare unsigned EAX to EBX, and copy the larger of the two into a ECX.

5/19/2019 Kenga©2019 Lecture notes 87


BSD 1308: Assembly language programming

9. Control structures
Jumps: The following is a list of jumps based on the Zero, Carry, Overflow, Sign, and Parity flags.

Example 2: Write code that jumps to a label if an integer is negative.


Hint: Jump to a label if the value in AL is not zero.
Solution: OR the byte with itself, then use the JNZ (jump if not zero) instruction.
al,al
jnz IsNotZero ; jump if not zero

5/19/2019 Kenga©2019 Lecture notes 88


BSD 1308: Assembly language programming

9. Control structures
Jumps: The following table shows a list of signed jumps based on comparison of signed
operands

Example 2: Jump to a label if signed EAX is greater than EBX.


Solution: Use CMP, followed by JG
cmp eax, ebx
jg Greater

5/19/2019 Kenga©2019 Lecture notes 89


BSD 1308: Assembly language programming

9. Control structures
Jumps: The following shows a list of unsigned jumps based on comparisons of unsigned operands.

Example : Jump to a label if unsigned EAX is greater than EBX.


Solution: Use CMP, followed by JA
cmp eax,ebx
ja Larger

5/19/2019 Kenga©2019 Lecture notes 90


BSD 1308: Assembly language programming

9. Control structures
Selection structure: A statement is executed based on a condition. Recall if..else?

Notice the use of cmp instruction.


Used in conjunction with
conditional jump

5/19/2019 Kenga©2019 Lecture notes 91


BSD 1308: Assembly language programming

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.

Notice the use of jne instruction.

5/19/2019 Kenga©2019 Lecture notes 92


BSD 1308: Assembly language programming

9. Control structures
Example 1: Program to print series of numbers in descending order.

5/19/2019 Kenga©2019 Lecture notes 93


BSD 1308: Assembly language programming

9. Control structures
Example 2: Program to print series of odd numbers in ascending order.

5/19/2019 Kenga©2019 Lecture notes 94


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 95


BSD 1308: Assembly language programming

10. Bit manipulation


Lesson outline:
• Overview
• Bit manipulation instructions
• Summary of instructions
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 96


BSD 1308: Assembly language programming

10. Bit manipulation


Overview
• Bit is the smallest unit of data in a computer.
• Bit manipulation is useful in data compression and encryption.
• Data is compressed by converting it from one representation to another, to reduce the
space.
• Data encryption is the process of translating data into a secret code(encoded data).
• Bit manipulation involves activities such as setting bits, clearing bits, inverting bits, testing
and comparing bits, extracting bits from a bit string, and inserting bits into a bit string.
Bit manipulation instructions
• Assembly language provides the instructions AND, OR, XOR, TEST, NOT, SHIFT and ROTATE
instructions which tests, sets, and clears the bits according to the need of the program.

5/19/2019 Kenga©2019 Lecture notes 97


BSD 1308: Assembly language programming

10. Bit manipulation


Bit manipulation instructions…continued
SHIFT: A shift moves the bits around in some data. A shift can be toward the left or toward the right.
Mainly when multiplying or dividing by 2.
ROTATE: Also called circular shift, is an operation similar to shift except that the bits that fall off at one
end are put back to the other end. This is useful if it is necessary to retain all the existing bits, and is
frequently used in digital cryptography.
AND: To clear one or more bits of a byte, word, or doubleword.
OR: To set one or more bits of a byte, word, or doubleword . To paste one or more bits of a byte, word,
or doubleword.
XOR: To toggle one or more bits of a byte, word, or doubleword.To initialize registers to zero.
NOT: To complement bits.
TEST: To test bits.

5/19/2019 Kenga©2019 Lecture notes 98


BSD 1308: Assembly language programming

10. Bit manipulation


Summary of bit manipulation instructions
Instruction Format Example
AND AND operand1, operand2 AND EAX,EBX
OR OR operand1, operand2 OR ECX,EDX
XOR XOR operand1, operand2 XOR EAX,EAX
TEST TEST operand1, operand2 TEST EAX,01H
NOT NOT operand1 NOT EAX
SHIFT LEFT SHL operand1,operand2 SHL EAX,EBX
SHIFT RIGHT SHR operant1,operand2 SHR EBX,ECX
ROTATE LEFT ROL operand ROL EAX,3
ROTATE RIGHT ROR operand ROR EBX,3

5/19/2019 Kenga©2019 Lecture notes 99


BSD 1308: Assembly language programming

10. Bit manipulation


Example 1: Program to manipulate bits using XOR,OR,AND

5/19/2019 Kenga©2019 Lecture notes 100


BSD 1308: Assembly language programming

10. Bit manipulation


Example 2: Program to manipulate bits using SHIFT,NEGATE and ROTATE.

5/19/2019 Kenga©2019 Lecture notes 101


BSD 1308: Assembly language programming

10. Bit manipulation


Revision questions:
a) Briefly explain the use of the following bit manipulation operators
b) Given the byte 10110010, write assembly language code to set both the second bit from the right
and the fourth bit from the right to 0.
c) Given the byte 10110010, write assembly language code to set both the second bit from the right
and the fourth bit from the right to 1.
d) Given the byte 10110010, write assembly language code to flip both the second bit from the right
and the fourth bit from the right.
e) Describe any two applications of bit manipulation.

5/19/2019 Kenga©2019 Lecture notes 102


BSD 1308: Assembly language programming

11. Masking operations


Lesson outline:
• Overview
• Masking operations
• Applications of masking
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 103


BSD 1308: Assembly language programming

11. Masking operations


Definition:
• A mask is a value used to force certain bits to zero or one within some other value.
• A mask typically affects certain bits in an operand (forcing them to zero or one) and leaves other bits
unaffected.
• The appropriate use of masks allows you to extract bits from a value, insert bits into a value, and
pack or unpacked a packed data type.
• The process of changing only the bit(s) you want without changing the rest of the bit(s) in the register
• Using a mask, multiple bits in a Byte, nibble, word (etc.) can be set either on, off or inverted from on
to off (or vice versa) in a single bitwise operation.
• AND operation: To extract a subset of the bits in the value
• OR operation: To set a subset of the bits in the value
• XOR operation: To toggle a subset of the bits in the value

5/19/2019 Kenga©2019 Lecture notes 104


BSD 1308: Assembly language programming

11. Masking operations


ANDing:
• More often in practice bits are masked off (masked to 0) than masked on (masked to 1). When a bit is ANDed
with a 0, the result is always 0, i.e. Y AND 0=0. To leave the other bits as they were originally, they can be ANDed
with 1, since Y AND 1=Y.
• Example. Given 10010101 consider a mask 11110000.
Value 1 0 0 1 0 1 0 1
AND Mask 1 1 1 1 0 0 0 0
Result 1 0 0 1 0 0 0 0

5/19/2019 Kenga©2019 Lecture notes 105


BSD 1308: Assembly language programming

11. Masking operations


ORing:
• To turn certain bits on, the bitwise OR operation can be used, following the principle that Y OR 1=1 and Y OR
0=Y. Therefore, to make sure a bit is on, OR can be used with a 1. To leave a bit unchanged, OR is used with a
0.
• Example. Given 10010101 consider a mask 11110000.
Value 1 0 0 1 0 1 0 1
OR Mask 1 1 1 1 0 0 0 0
Result 1 1 1 1 0 1 0 1

5/19/2019 Kenga©2019 Lecture notes 106


BSD 1308: Assembly language programming

11. Masking operations


XORing
• Used in toggling bit values.
• XOR returns 1 if and only if an odd number of bits are 1.
• Therefore, if two corresponding bits are 1, the result will be a 0, but if only one of them is 1, the result will be 1.
• Therefore inversion of the values of bits is done by XORing them with a 1.
• If the original bit was 1, it returns 1 XOR 1=0. If the original bit was 0 it returns 0 XOR 1=1.
• Example. Given 10010101 consider a mask 11110000.
Value 1 0 0 1 0 1 0 1
OR Mask 1 1 1 1 0 0 0 0
Result 0 1 1 0 0 1 0 1

5/19/2019 Kenga©2019 Lecture notes 107


BSD 1308: Assembly language programming

11. Masking operations


Example: A simple assembly program to demonstrate masking using ORing, ANDing and XORing.

5/19/2019 Kenga©2019 Lecture notes 108


BSD 1308: Assembly language programming

11. Masking operations


Applications of bit masking
Bitmasks are generally used as a way to isolate specific bits in a sequence or verify them against a
pattern.
• Bitmasks are used with IP addresses.
• Bitmasks are used in efficient data storage.
• Bitmasks are used to pass a set of named boolean arguments to a function.
• Bitmasks are used in image steganography.
• Bitmasks are used in cryptography.

5/19/2019 Kenga©2019 Lecture notes 109


BSD 1308: Assembly language programming

11. Masking operations


Revision questions.
a) Explain two uses of masking as used in bit operations.
b) Given B=56 and a mask C=42, determine the results of:
i. B AND C
ii. B OR C
iii. B XOR C
c) Explain you can using masking to determine if a number is even or odd.
d) State and explain the bitwise operator suitable for turning off a particular bit in a number.
e) State and explain the bitwise operator suitable for turning on a particular bit in a number.

5/19/2019 Kenga©2019 Lecture notes 110


BSD 1308: Assembly language programming

12. Arrays
Lesson outline:
• Overview
• Array declarations
• Accessing array elements
• Using arrays in assembly programs
• Advantages of arrays
• Disadvantages of arrays
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 111


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 112


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 114


BSD 1308: Assembly language programming

12. Arrays
Example: A program to store 8 numbers in an array, then add and display their sum.

5/19/2019 Kenga©2019 Lecture notes 115


BSD 1308: Assembly language programming

12. Arrays
Example: A program to store 11 numbers in an array, then display their values.

5/19/2019 Kenga©2019 Lecture notes 116


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 117


BSD 1308: Assembly language programming

13. Procedures
Lesson outline:
• Overview
• Syntax of a procedure
• Calling a procedure
• Returning from procedure
• Uses of procedures
• Revision questions

5/19/2019 Kenga©2019 Lecture notes 118


BSD 1308: Assembly language programming

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:

• The called procedure receives the call(values or addresses).


• The called procedure then performs the required processing.
• The called procedure then returns the control to the calling procedure by using the RET instruction.
• Syntax: ret

5/19/2019 Kenga©2019 Lecture notes 120


BSD 1308: Assembly language programming

13. Procedures
Consider the procedures below.

5/19/2019 Kenga©2019 Lecture notes 121


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 122


BSD 1308: Assembly language programming

13. Procedures
Example: A program that uses two procedures, that prints some text/string.

5/19/2019 Kenga©2019 Lecture notes 123


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 125


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 126


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 127


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 128


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 129


BSD 1308: Assembly language programming

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.

5/19/2019 Kenga©2019 Lecture notes 130


BSD 1308: Assembly language programming

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

5/19/2019 Kenga©2019 Lecture notes 132

You might also like