Cte 221 Note
Cte 221 Note
2 Contributes towards the size of the Does not contribute to program size
program (opcode length)
4 Instruction are assembled into machine Directives are only used at assembly
code and eventually linked into the time because they do not generate
final executable code.
EXERCISE ONE
1. What is Assembly Language?
2 Differentiate between Assembly Language and Machine Language
3 Enumerate the difference between AN INSTRUCTION and A DIRECTIVE in
Assembly Language?
4 List the Advantages and Disadvantages of HLL respectively?
Most modern computer systems do not represent numeric values using the decimal system.
Instead, they use a binary or two’s complement numbering system. To understand the
limitations of computer arithmetic, one must understand how computers represent numbers.
With two such levels we can represent exactly two different values. These could be any two
different values, but by convention we use the values zero and one. These two values,
coincidentally, correspond to the two digits used by the binary numbering system.
The binary numbering system works just like the decimal numbering system, with two
exceptions: binary only allows the digits 0 and 1 (rather than 0-9), and binary uses powers of
two rather than powers of ten. Therefore, it is very easy to convert a binary number to decimal.
For each "1" in the binary string, add in 2**n where "n" is the zero-based position of the binary
digit. For example, the binary value 11001010 represents:
=128 + 64 + 8 + 2
Octal numbers are numbers to base 8. The primary advantage of the octal number system is
the ease with which conversion can be made between binary and decimal numbers. Octal is
often used as shorthand for binary numbers because of its easy conversion. The octal
numbering system is shown below;
0 001
1 001
2 010
3 011
4 100
5 101
6 110
7 111
Example 6238
6X82+2X81+3X80
384+16+3
=403
The decimal (base 10) numbering system has been used for so long that people take it for
granted. When you see a number like “123”, you don’t think about the value 123, rather,
you generate a mental image of how many items this value represents in reality, however,
the number 123 represents” 1*102 + 2*101 + 3*100 or 100+20+3 Each digit appearing to
To convert decimal to binary is slightly more difficult. You must find those powers of two
which, when added together, produce the decimal result. The easiest method is to work from
a large power of two down to 2**0. Consider the decimal value 1359:
2**10=1024, 2**11=2048. So 1024 is the largest power of two less than 1359. Subtract
1024 from 1359 and begin the binary value on the left with a "1" digit. Binary = "1",
Decimal result is 1359 - 1024 = 335.
The next lower power of two (2**9= 512) is greater than the result from above, so add a "0"
to the end of the binary string. Binary = "10", Decimal result is still 335.
The next lower power of two is 256 (2**8). Subtract this from 335 and add a "1" digit to the
end of the binary number. Binary = "101", Decimal result is 79.
128 (2**7) is greater than 79, so tack a "0" to the end of the binary string. Binary = "1010",
Decimal result remains 79.
16 (2**4) is greater than the remainder so far, so append a "0" to the end of the binary string.
Binary = "1010100", Decimal result is 15.
2**3(eight) is less than 15, so stick another "1" digit on the end of the binary string.
Binary = "10101001", Decimal result is 7.
2**2 is less than seven, so subtract four from seven and append another one to the binary
string. Binary = "101010011", decimal result is 3.
2**1 is less than three, so append a one to the end of the binary string and subtract two from
the decimal value. Binary = "1010100111", Decimal result is now 1.
Finally, the decimal result is one, which is2**0, so add a final "1" to the end of the binary
string. The final binary result is "10101001111"
Each hexadecimal digit can represent one of sixteen values between 0 and 15. Since
there are only ten decimal digits, we need to invent six additional digits to represent the
values in the range 10 through 15. Rather than create new symbols for these digits,
we'll use the letters A through F.
Example: 1AB16
Convert fromX16 X10
(1AB) 16
1 x 162 + 10 x 161 + 11 x 160
256 + 160 + 11
= 42710
10 | P a g e CTE 221
As you can see, hexadecimal numbers are compact and easy to read. In addition, you can
easily convert between hexadecimal and binary.
Consider the following table:
Binary/Hex Conversion
Binary Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
This table provides all the information you'll ever need to convert any hexadecimal number
into a binary number or vice versa.
To convert a hexadecimal number into a binary number, simply substitute the corresponding
four bits for each hexadecimal digit in the number.
Example
Convert 0ABCDh into its binary value
11 | P a g e CTE 221
Answer:
Convert each hexadecimal digit according to the table above:
0 A B C D Hexadecimal
= (0000 1010 1011 1100 1101)2
To convert a binary number into hexadecimal format is almost as easy. The first step is to pad
the binary number with zeros to make sure that there is a multiple of four bits in the number.
Example: Find the hexadecimal equivalent, given the binary number 1011001010?
Step 1: add two bits to the left of the number so that it contains 12 bits.
The converted binary value is 001011001010
Step 2: separate the binary value into groups of four bits
0010 1100 1010
Step 3: look up these binary values in the table above and substitute the appropriate
hexadecimal digits,
0010 1100 1010.
2 C A.
12 | P a g e CTE 221
1010 12 10 A
1011 13 11 B
1100 14 12 C
1101 15 13 D
1110 16 14 E
1111 17 15 F
Examples:
(1 × 2-2)
Solution:
Step 1: Separate the binary numbers into groups of 3 bits on both sides of binary point.
Step 2: Here, on right side of binary point, the last group is having only 2 bits. So, include one
zero on extreme side in order to make it as group of 3 bits.
13 | P a g e CTE 221
Step 3 − Write the octal digits corresponding to each group of 3 bits.
⇒ 010110111.1101002 = 267.648
11 0001.1010 1
Here, the first group is having only 2 bits. So, include two zeros on extreme side in order to
make it as group of 4 bits.
Similarly, include three zeros on extreme side in order to make the last group also as group of
4 bits.
1. Hexadecimal Addition
Example:
Solution:
In hexadecimal addition of alphabets, we add the corresponding numbers they represent and
then subtract the result from 16, then generate a carry of 1 to the next set of numbers.
Here, 5+2=7
C+F=12+15=27-16=11 i. e B (carry 1)
B + B+1(carry) = 11 +11= 23-16 = 7 (carry 1) again
5+2+1(carry) =8
14 | P a g e CTE 221
HEXADECIMAL ADDITION is done as follows:
5BC
+ 2BF
________
87B
EXERCISE TWO
1. The decimal equivalent of (11101010) 2
15 | P a g e CTE 221
CHAPTER THREE
3.0 TYPES OF ENCODING
When numbers, letters and words are represented by a special group of symbols, this is called
“Encoding” and the group of symbol encoded is called a “code”. Any decimal number can be
represented by an equivalent binary number. When a decimal number is represented by its
equivalent binary number, it is called “straight binary coding”.
16 | P a g e CTE 221
E.g. 87410 = 1000011101002
94310 = 1001010000112
Only the four bits binary numbers from 0000 through 1001 are used for binary coded decimal.
The BCD code does not use the numbers 10, 11, 12, 13, 14, 15. In other words, 10 of the 16
possible 4 bits binary codes are used. If any of these forbidden 4 bits number ever occurs in a
machine using the BCD, it is usually an indication that an error has occurred.
EXERCISE THREE
17 | P a g e CTE 221
CHAPTER FOUR
Most data structures are abstract structures and are implemented by the programmer
with a series of assembly language instructions. Many cardinal data types (bits, bit strings,
bit slices, binary integers, binary floating point numbers, binary encoded decimals, binary
addresses, characters, etc.) are implemented directly in hardware for at least parts of the
instruction set. Some processors also implement some data structures in hardware for
some instructions — for example, most processors have a few instructions for directly
manipulating character strings.
An assembly language programmer has to know how the hardware implements these
cardinal data types. Some examples: Two basic issues are bit ordering (big endian or little
endian) and number of bits (or bytes). The assembly language programmer must also pay
attention to word length and optimum (or required) addressing boundaries. Composite
data types will also include details of hardware implementation, such as how many bits of
mantissa, characteristic, and sign, as well as their order.
18 | P a g e CTE 221
slower) hardware for performing addition, subtraction, and any binary integer
operations other than complement (which only requires a sign bit change).
In sign magnitude, the sign bit for positive is represented by 0 and the sign bit for
negative is represented by 1.
Examples:
Answer:
2. Convert -52 to binary using an 8 bits machine Answer: The binary equivalence of
52 is 110100 but 1 is used to represent negative magnitude, hence 1 is added to the
front of this binary equivalence. This makes a total of 7bits, since we are working
on an eight bit machine, we have to pad the numbers with 0 so as to make it a total
of 8bits. In this case, the sign bit has to come first and the padded 0 follows.
19 | P a g e CTE 221
bit (technically, the carry bit is always added to the least significant bit, but when it
is zero, the add has no effect). The ripple effect of adding the carry bit can almost
double the time to do an addition. And there are still two zeros, a positive zero (all
zero bits) and a negative zero (all one bits).
The 1’s complement form of any binary number is simply by changing each 0 in the
number to a 1 and vice versa.
Answer: -7 in the actual representation without considering the machine bit is 1111.
To change this to 1’s complement, the sign bit has to be retained and other bits have
to be inverted. Thus, the answer is: 1000. 1 denotes the sign bit.
The actual magnitude representation of -7.25 is 1111.01 but retaining the sign bits
and inverting the other bits gives: 1000.10
In two’s complement representation, positive numbers are represented in the “normal” manner
(same as unsigned integers with a zero sign bit), while negative numbers are represented by
complementing all of the bits of the absolute value of the number and adding one. Negation of
a negative number in two’s complement representation is accomplished by complementing all
of the bits and adding one. Addition is performed by adding the two numbers as unsigned
integers and ignoring the carry. Two’s complement has the further advantage that there is only
one zero (all zero bits). Two’s complement representation does result in one more negative
number (all one bits) than positive numbers.
20 | P a g e CTE 221
Two’s complement is used in just about every binary computer ever made. Most processors
have one more negative number than positive numbers. Some processors use the “extra”
neagtive number (all one bits) as a special indicator, depicting invalid results, not a number
(NaN), or other special codes. 2’s complement is used to represent negative numbers because
it allows us to perform the operation of subtraction by actually performing addition. The 2’s
complement of a binary number is the addition of 1 to the rightmost bit of its 1’s complement
equivalence.
Examples
11001011
+ 1
11001100
2. Convert -419 to 2’s complement and hence convert the result to hexadecimal
The sign magnitude representation of -419 on a 16 bit machine is
1000000110100011
1111111001011100
+ 1
1111111001011101
21 | P a g e CTE 221
Dividing the resulting bits into four 1111 1110 0101 1101gives an
hexadecimal equivalence of FE5D16
Answer:
7777
-7245
0532
+1
0533
is 0533
= 01001
00100
01101
22 | P a g e CTE 221
EXERCISE FOUR:
2. Find the 1’s complement of -467 and convert the answer to hexadecimal.
23 | P a g e CTE 221
CHAPTER FIVE
5.0 REGISTERS
To speed up the processor operations, the processor includes some internal memory storage
locations, called registers. Namely:
Data registers,
Pointer registers, and
Index registers.
DATA REGISTER
AX, BX, CX, DX which is divided into higher and lower halves i.e
BX (BH, BL),
CX (CH, CL),
DX (DH, DL)
24 | P a g e CTE 221
POINTER REGISTER:
As 32- bit we have EIP (Instruction pointer), ESP (stack pointer), EBP (base pointer)
As 16- bit we have IP (source index pointer &destination index pointer), SP and BP
Control registers
Segment registers.
25 | P a g e CTE 221
x: memory address
When the processor gets the numeric data from memory to register, it again reverses the bytes.
There are two kinds of memory addresses −
Absolute address - a direct reference of specific location.
Segment address (or offset) - starting address of a memory segment with the offset value.
The three basic modes of addressing are −
I. Register addressing
II. Immediate addressing
III. Memory addressing
Example:
i. Determine the value of the Accumulator for the instruction below:
MOV A, #7FH
ADD A, #01H
Answer:
Accumulator: 80H
ii. Write an assembly language program to copy the value of 50H into RAM
location 60H to 65H using; Register indirect addressing mode with a loop.
Answer:
MOV A, #50 ; A = 50H
26 | P a g e CTE 221
5.2 ASSEMBLY - BASIC SYNTAX
An assembly program can be divided into three sections −
I. The data section:- The data section is used for declaring initialized data or constants
II. The bss section:-The bss section is used for declaring variables, and
III. The text section:- The text section is used for keeping the actual code. This section
must begin with the declaration global _start.
Below is a simple assembly language program to print “Hello World”:
.mode1 small
.stack 100h
.code
Start:
Mov ds, ax
Int 21h
Int 21h
End start.
27 | P a g e CTE 221
5.3 ASSEMBLY - LOGICAL INSTRUCTIONS
The processor instruction set provides the instructions AND, OR, XOR, TEST, and NOT
Boolean logic, which tests, sets, and clears the bits according to the need of the program.
Example
Assume that RAM location 30 – 34 have the following values; write a program to
find the sum of the values. [Note Register (A) should contain the low byte and
Register (R7) should contain the high byte, all values are in hex].
Answer:
CLR A ;A=0
28 | P a g e CTE 221
INC R0 ; increment pointer
EXERCISE FIVE
1. Write a simple Assembly Language program to Print ‘Hello World’ on the screen
2. Explain Assembly Register
3. List any five (5) General purpose Register for x86 microprocessor.
4. Highlight five (5) functions of Addressing mode in the 8086 microprocessor
5. Explain Register Addressing mode with appropriate example
29 | P a g e CTE 221
6.0 CHAPTER SIX
MICROPROCESSOR
These are the processors which are designed for some special purposes
An instruction set is a list of all the instructions, and all their variations, that a processor can
execute.
30 | P a g e CTE 221
An instruction set, or instruction set architecture (ISA), is the part of the computer
architecture related to programming, including the native data types, instructions, registers,
addressing modes, memory architecture, interrupt and exception handling, and external I/O.
An ISA includes a specification of the set of opcodes (machine language), the native
commands implemented by a particular CPU design.
The acronym RISC (pronounced risk), for reduced instruction set computing, represents a
CPU design strategy emphasizing the insight that simplified instructions that "do less" may
still provide for higher performance if this simplicity can be utilized to make instructions
execute very quickly. Many proposals for a "precise" definition have been attempted, and the
term is being slowly replaced by the more descriptive load-store architecture. Well known
RISC families include Alpha, ARC, ARM, AVR, MIPS, PA-RISC, Power Architecture
(including PowerPC), SuperH, and SPARC.
RISC stands for Reduced Instruction Set Computer. It is designed to reduce the execution
time by simplifying the instruction set of the computer
CISC stands for Complex Instruction Set Computer. It is designed to minimize the number
of instructions per program, ignoring the number of cycles per instruction
31 | P a g e CTE 221
TEST EXECISE
1. Write a simple Assembly Language program to Print ‘Hello World’ on the screen
17. Using a 5-bit machine add -9 and +4 and find its 2’s complement.
32 | P a g e CTE 221