Вы находитесь на странице: 1из 20

CS225 Lab Manual

Course Description:
This course concentrates on the practical part of Computer Organization by using
Assembly language. This course allows students to practice writing programs based on the
concepts the will learn through the course by giving the students different types of
problems to be solved using MASM (Macro Assembler)

Course Objectives:
1. Teach students basic principles about computer architecture, machine language,
and low-level programming.
2. Teach students enough assembly language to enhance their knowledge on today's
most widely used microcomputer family.
3. Improving students systems programming skills through programming exercises
carried out by students.
4. Students are expected to implement solutions to problems using the concepts they
will take through the course.

Learning Outcomes:
After completing this course the student expect to:
Solve a problem and write a program for this problem using assembly language by using
MASM and the concepts they will take through the course.

Course Plan:
1.
2.
3.
4.
5.
6.
7.
8.

Basic Concepts
Assembly Language Fundamentals
Data Transfers, Addressing, and Arithmetic
Procedures
Conditional Processing
Integer Arithmetic
Strings and Arrays
16-Bit MS-DOS Programming

Introduction:
What Is Assembly Language?
Assembly language is a level lays between high level language and machine code, it reflects the
instruction set as well as the processor architecture. In this course we are focusing on Intel 32-bit
processors. We will show how to code in assembly language which will be translated in to
machine code later. Many types of assemblers are used for Intel processors, such free assembler
as: NASM (Netwide Assembler), MASM (Microsoft Assembler), and TASM (Borland Turbo
Assembler).
Coding with assembly language provides time-efficient and space-efficient codes. Assembly
language allows to access computer hardware easily. Assembly language composed assemble
and linker. An assembler is responsible to translate assembly program into object file. A linker
is responsible to aggregates multiple object files generated by an assembler into an executable
process.
The following figure shows P6 Family Processor Basic Execution Environment

general purpose registers[2]

Segment register[2]

Segment registers [2]

Data sizes [2]

2- Number Systems Used in Computers


We will talk about numbering system such as binary system, hexadecimal system, decimal
system and octal system. Binary numbers mainly deals with two digits, which are zero and one.
Example of binary number is (10110011)2 , 2 means base 2, and it is equivalent to (179)10 , 10
means base 10.

1. Converting from decimal number into binary number


To convert the whole number portion to binary, use successive division by 2 until the quotient
is 0. The remainders form each successive division compose the answer, with the first
remainder as the least significant bit (LSB) and the last as the most significant bit (MSB).
To Convert 201210 to binary number, use repeated division by 2 and collect reminders until the
quotient becomes 0. The collected reminders are the answer on condition the first reminder is
the least significant bit and the last reminder is the most significant bit. See the following
example.
2012 / 2 = 1006 remainder 0 (LSB)
/ 2 = 503 remainder 0
/ 2 = 251 remainder 1
/ 2 = 125 remainder 1
/ 2 = 62 remainder 1
/ 2 = 31 remainder 0
/ 2 = 15 remainder 1
/ 2 = 7 remainder 1
/ 2 = 3 remainder 1
/ 2 = 1 remainder 1
/ 2 = 0 remainder 1 (MSB)
201210 = 111110111002
To convert decimal fractions to binary, multiply it by 2, collect non-fraction 0 or 1, then replace
it by zero, repeat this actions until the whole value becomes 0. The collected 0s and 1s are the
answer on condition the first value is the most significant bit and the last 0 or 1 is the least
significant bit. See the following example.
Example: Convert 0.62510 to binary
6

.625 2 = 1.25

.25 2

= 0.50

.5 2

= 1.0

(MSB)

(LSB)

0.62510 = 0.1012

2. Converting from decimal number into hexadecimal number


Hexadecimal system contains the following digits: 0, 1,2, 3, 4, 5, 6, 7, 8, 9. A, B, C, D, E and F.
the following table shows some binary numbers and their equivalence hexadecimal values.
Table 1: binary and hexadecimal values
Binary
0000
0100
1000
1100

Hexadicimal
0
1
2
C

Binary
0001
0101
1001
1101

Hexadicimal
1
5
9
D

Binary
0010

0110
1010
1110

Hexadicimal
2
6
A
E

Binary
0011
0111
1011
1111

Hexadicimal
3
7
B
F

To convert from decimal value into binary value, use successive division by 16 until the
quotient becomes 0. The remainders form each successive division compose the answer, with
the first remainder as the least significant bit (LSB) and the last as the most significant bit
(MSB).
To Convert 201210 to hexadicimal number, use repeated division by 16 and collect reminders
until the quotient becomes 0. The collected reminders are the answer on condition the first
reminder is the least significant bit (LSB) and the last reminder is the most significant bit
(MSB). See the following example.
2012 / 16 = 125 remainder C (LSB)
125 / 2 = 7 remainder D
7 / 2 = 0 remainder 7 (MSB)
201210 = 7DC16
An easier way to convert from decimal to hexadecimal is get binary value first and then
convert the binary number into hexadecimal using table 1.
201210 = 111110111002

To do this, divide the binary number into multiple of four binary digits starting from right to
left: (111 1101 1100)2 . if the last part is less than four digits, then pad it with zeros: (0111 1101

1100)2 = ( 7DC)16
To convert decimal fractions to hexadecimal value, multiply it by 16, and collect non-fraction
value, then replace it by zero. Repeat this actions until the whole value becomes 0. The collected
numbers are the answer on condition the first value is the most significant bit and the last
number is the least significant bit. See the following example.
Example: Convert 0.039062510 to hexadecimal
0. 0.0390625 16

= 0.625

0.625 x 16 = 10.0

(MSB)

A (LSB)

0.039062510 = .0A16

3. Converting from decimal number into octal number


To Convert 201210 to octal number, use repeated division by 8 and collect the reminders until
the quotient becomes 0. The collected reminders are the answer on condition the first
reminder is the least significant bit (LSB) and the last reminder is the most significant bit
(MSB). See the following example.
2012 / 8 = 251 remainder 4 (LSB)
251 / 8 = 31 remainder 3
31 / 8 = 3 remainder 7
3 / 8 = 0 remainder 3 (MSB)
201210 = 43338
An easier way to convert from decimal to hexadecimal is get binary value first and then
convert the binary number into octal using table 2.
Table 2: binary and equivalent octal values
Binary
000
100

Hexadicimal
0
1

Binary
001
101

Hexadicimal
1
5

Binary
010

110

201210 = 111110111002

Hexadicimal
2
6

Binary
011
111

Hexadicimal
3
7

To do this, divide the binary number into multiple of three binary digits starting from right to
left: (11 111 011 100)2 . if the last part is less than three digits, then pad it with zeros:

(011 111 011 100)2 = ( 3734)16

To convert decimal fractions to hexadecimal value, multiply it by 8, and collect non-fraction


value, then replace it by zero. Repeat this actions until the whole value becomes 0. The collected
numbers are the answer on condition the first value is the most significant bit and the last
number is the least significant bit. See the following example.
Example: Convert 0.039062510 to octal
0. 0.0390625 8
0.03125
0.25

= 0.03125

(MSB)

x 8 = 0.25

x 8

2 (LSB)

2.0

0.039062510 = .00216

4. Converting binary value into decimal value


To convert a binary value into decimal value, multiplies each digit by its corresponding power
of 2 and then add them. Starting from right to left, power starts with 0, and then 1, and so on.
For example, (100101000101)2 = 1 * 20 + 0 * 21 + 1 * 22 + 0 * 23 + 0 * 24 + 0 * 25 + 1 * 26 + 0 *
27 + 1 * 28 + 0 * 29 + 0 * 210 + 1 * 211 = 1 + 4 + 64 + 256 + 2048 = 2373
To convert fractioned binary value into decimal value, multiplies each digit by its
corresponding power of 2 and then add them. Starting from left to right after the fraction point
., power starts with -1, and then -2, and so on. For example, (0.1001)2 = 1 * 2-1 + 0 * 2-1 + 0 *
2-2 + 1 * 2-3 = .5 + 0.1250= 0.625.

5. Converting hexadecimal value into decimal value


To convert a hexadecimal value into decimal value, multiplies each digit by its corresponding
power of 16 and then adds them. Starting from right to left, power starts with 0, and then 1,
and so on. For example, (A13F)16 = F * 160 + 3 * 161 + 1 * 162 + A * 163 = 15 * 16 + 3 * 16 + 1
* 256 + 10 * 4096 = 41279.
To convert fractioned hexadecimal value into decimal value, multiplies each digit by its
corresponding power of 16 and then adds them. Starting from left to right after the fraction
9

point ., power starts with -1, and then -2, and so on. For example, (0.EF1)16 = E * 16-1 + F *
16-1 + 1 * 16-2 = (14/16) + (15/256) + (1/4096)= 0.933837890625.

6. Converting octal value into decimal value


To convert a octal value into decimal value, multiplies each digit by its corresponding power of
8 and then adds them. Starting from right to left, power starts with 0, and then 1, and so on.
For example, (A13F)16 = F * 160 + 3 * 161 + 1 * 162 + A * 163 = 15 * 16 + 3 * 16 + 1 * 256 + 10
* 4096 = 41279.
To convert fractioned octal value into decimal value, multiplies each digit by its corresponding
power of 8 and then adds them. Starting from left to right after the fraction point ., power
starts with -1, and then -2, and so on. For example, (0.734)8 = 7 * 8-1 + 3 * 8-1 + 4 * 8-2 = (7/8)
+ (3/64) + (4/512) = 0.9296875.

10

3- Binary Arithmetic Operations


a. Unsigned addition
Decimal number addition is done by adding each pair of digits. The carry is added with the
next pair of digits and so on. Adding two binary numbers are done in the same way by adding
each pair of bits together, the resulted carry is added with the next pair.

1
X
Y

211
343
554

1
0
1
0

1
1
0
0

0
1
1
0

1
0
0
1

0
1
1
0

1
0
0
1

1
0
1
0

1
1
1
1

Carry
1 +
1
0

Hexadecimal and octal addition is done by the same procedure. However we can convert each
hexadecimal and octal numbers into corresponding binary number. By then we can do simpler
addition.

1
( D3) 16 +
( 57) 16
( 12A) 16

1
1
0
0

0
1
1
0

1
0
0
1

0
1
1
0

1
0
0
1

1
0
1
0

1
1
1
1

Carry
1 +
1
0

b. Signed Integers

Signed integers are represented by reserving the most significant bit. If the MSB is equal to 1,
then the signed integer number is negative, otherwise, it is positive. In this section the student
need to learn about the range of signed fixed number of bits. See the figure below. Table 3
shows the code representation for signed and unsigned numbers for four bits. You will notice
that signed four-bit ranges between -7 and +7 for 1st complement, and ranges from -8 to +7 for
2nd complement.

11

Table 3 the code representation for signed and unsigned numbers


4-bit binary
value
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

Unsigned
decimal value
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Signed decimal value


1st complement
2nd complement
+0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
-7
-8
-6
-7
-5
-6
-4
-5
-3
-4
-2
-3
-1
-2
-0
-1

Signed number is represented either by ones complement or by twos complement. To


get the first complement of any binary number is accomplished by negating each bit of
the corresponding binary number. So, the 1st complement of (10001010010)2 is equal to
(01110001101)2, the 2nd complement is represented by adding one to the 1st complement.
Then (01110001101)2 + 1 = (01110001110)2. as a result (01110001110)2 is the second
complement of (10001010010)2.

c. Convert Signed Binary to Decimal

12

If the MSB is equal to 0, then the sign of the binary code is positive, in this case we
follow the same procedures we did in section Converting binary value into decimal
value to get it\s value.
If the MSB bit is 1 then the sign of the binary number is negative. To get its positive
decimal equivalent we need to convert it into twos complemented, afterward; convert
the result into decimal value. Ex, if the number (1110 0101)2 is represented in the 2nd
complement, the binary positive equivalent value is (0001 1011 + 1 = 0001 1011)2. The
decimal value for (0001 1011)2 is (27)10, so the decimal equivalent of (1110 0101)2 is (27)10.
There are basically two methods summarized in table 3 for representing signed
numbers: one's complement, and two's complement.

d. signed addition
Suppose we want to subtract (00101011)2 from (01110001)2
To accomplish the operation we need to
convert (00101011)2 into negative value by
getting the 2nd complement of the binary
number, by then adding the two values
leads to the target

So subtracting [(00101011)2 which is equal 43] from [(01110001)2 which is equal 113] yield
to [(01000110) 2 which is equal 70]. The result is correct if the carry-in to sign bit is equal to
carry-out from the sign bit, otherwise an overflow bit is set to one.

13

e. Number of bits versus number of states


One bits is able to store either 0 or 1, in another words, we can say that 1 bit is able to
address (21) 2 states. 2 bits is able to address (22) 4 states, which are 00, 01, 10 and 11. 8 bits
forms a byte, which is the smallest addressable data item. A byte is able to address (28) 256
states. The range of unsigned values that can be derived from 8 bits are numbers between
0 and (28-1) 255. The range of signed values using 2nd complement that can be derived from
8 bits are numbers between (-(27)) -128 and (27-1) +127, since that the MSB bit is reserved
for the sign and the rest of 7 bits are reserved for the value. Table 4 shows the range of
unsigned and signed numbers.
Table 4 Range of unsigned and signed numbers
Byte size

1 byte
2 bytes
4 bytes
8 bytes
10 bytes

Range of
Unsigned
decimal values
0 to (28-1)
0 to (216-1)
0 to (232-1)
0 to (264-1)
0 to (280-1)

Range of Signed decimal values


1st complement
2nd complement
-(27-1) to +(27-1)
-(215-1) to +(215-1)
-(231-1) to +(231-1)
-(263-1) to +(263-1)
-(279-1) to +(279-1)

-(27) to +(27-1)
-(215) to +(215-1)
-(231) to +(231-1)
-(263) to +(263-1)
-(279) to +(279-1)

Try this:
What is the largest unsigned integer that may be stored in 30 bits?
What is the largest signed integer using 2nd complement that may be stored in 30 bits?

14

Section 2:
a) The Intel 80386 Processor
Intel 8286 Processor consists of general purpose registers eax, ebx, and so on, see the
figure below. CPU registers are very fast memory. Eax, ebx, ecx and edx are trated as 8
bits, 16 bits and 32 bits.

32-bit General-Purpose Registers


EAX

EBP

EBX

ESP

ECX

ESI

EDX

EDI

16-bit Segment Registers


EFLAGS
EIP

CS

ES

SS

FS

DS

GS

EAX, AX and AL or AH can be reached individually, EBX, ECX, and EDX have the
same structure. AX means the accumulator register, BX means the base address register,
CX means the counter register and DX means the data register. EAX means extended
AX, and so on for EBX, ECX, and EDX. Extended instruction pointer register (EIP)
contains the address of the next instruction to execute.
b) The x86 Instruction Set
a. MOV instruction
mov instruction used to transfer data between registers, from memory to a register or
from register to memory; but not between memoirs. The mov instruction moves the value
stored in operand2 to the operand1, so the first operand is the destination and the second
operand is the source.
operand2 operand1

mov operand1, operand2


mov reg8,16,32, reg8,16,32
mov reg8,16,32, memory 8,16,32
mov reg8,16,32, constant
mov memory8,16,32, reg8,16,32
mov memory8,16,32, constant
Ex:
15

Mov ax, bx

; move the value stored in register bx into register ax, note that both the
; source and the destination storage must have the same size.
Mov ebx, ecx ; move the value stored in register ecx into register ebx.
Mov dl, dh
; move the value stored in register dh into register dl.
Mov al, cl
; move the value stored in register cl into register al.
b. The add instruction
The add instruction adds the value stored in operand2 into the value stored in operand1,
storing the result in operand1.
add operand1, operand2

operand1 = operand1 + operand2

add reg8,16,32, reg8,16,32


add reg8,16,32, memory 8,16,32
add reg8,16,32, constant
add memory8,16,32, reg8,16,32
add memory8,16,32, constant
Ex:
add ax, bx

; add the value stored in register bx into register ax, note that both the
; source and the destination storage must have the same size.
add ebx, ecx ; add the value stored in register ecx into register ebx.
add dl, dh
; add the value stored in register dh into register dl.
add al, cl
; add the value stored in register cl into register al.
c. The sub instruction
The sub instruction subtracts the value of the operand2 from the value stored in operand1,
storing the result in operand1.
sub operand1, operand2

operand1 = operand1 - operand2

sub reg8,16,32, reg8,16,32


sub reg8,16,32, memory 8,16,32
sub reg8,16,32, constant
sub memory8,16,32, reg8,16,32
sub memory8,16,32, constant
Ex:
sub ax, bx

; subtract the value stored in register bx from the value stored in register
; ax, and then store the result in ax. Note that both the source and the
16

; destination storage must have the same size.


sub ebx, ecx ; subtract the value stored in register ecx from the value stored in register
; ebx value.
sub dl, dh
; subtract the value stored in register dh from the value stored in register
; dl.
sub al, cl
; subtract the value stored in register cl from the value stored from register
; al.
d. The sub instruction
The cmp instruction is used to compare between operand1 and operand2. The result of
the comparison affect the flag register for subsequent use for conditional jump
instruction.
cmp operand1, operand2

compare operand1with operand2 Flag-reg

cmp reg8,16,32, reg8,16,32


cmp reg8,16,32, memory 8,16,32
cmp reg8,16,32, constant
cmp memory8,16,32, reg8,16,32
cmp memory8,16,32, constant
Ex:
cmp ax, bx

; compare the value stored in register bx with the value stored in register
; ax. Note that both the source and the destination storage must have the
; same size.
cmp ebx, ecx ; compare the value stored in register ecx with the value stored in register
; ebx.
cmp dl, dh
; compare the value stored in register dh with the value stored in register
; dl.
cmp al, cl
; compare the value stored in register cl with the value stored in register al.
e. The and instruction
The and instructions is used to bitwise and between operand1 and operand2, storing the
result into operand1.
and operand1, operand2

operand1 = operand1 & operand2

and reg8,16,32, reg8,16,32


and reg8,16,32, memory 8,16,32
17

and reg8,16,32, constant


and memory8,16,32, reg8,16,32
and memory8,16,32, constant
Ex:
and ax, bx

; and the value stored in register bx with the value stored in register
; ax, storing the result in register ax. Note that both the source and the
; destination storage must have the same size.
and ebx, ecx ; and the value stored in register ecx with the value stored in register
; ebx, storing the result in register ebx.
and dl, dh
; and the value stored in register dh with the value stored in register
; dl, storing the result in register dl.
and al, cl
; and the value stored in register cl with the value stored in register al,
; storing the result in register ax.
f. The or instruction
The and instructions is used to bitwise and between operand1 and operand2, storing the
result into operand1.
or operand1, operand2

operand1 = operand1 | operand2

or reg8,16,32, reg8,16,32
or reg8,16,32, memory 8,16,32
or reg8,16,32, constant
or memory8,16,32, reg8,16,32
or memory8,16,32, constant
Ex:
or ax, bx

; or the value stored in register bx with the value stored in register


; ax, storing the result in register ax. Note that both the source and the
; destination storage must have the same size.
or ebx, ecx ; or the value stored in register ecx with the value stored in register
; ebx, storing the result in register ebx.
or dl, dh
; or the value stored in register dh with the value stored in register
; dl, storing the result in register dl.
or al, cl
; or the value stored in register cl with the value stored in register al,
; storing the result in register ax.

18

g. The not instruction


The not instruction is used to get the 1st complement of the value stored in operand1.
not operand1

operand1 = first-complement(operand1)

not reg8,16,32
not memory8,16,32
Ex:
not ax
not ebx
not dl
not al
Not [mem]

; get the first complement of ax.


; get the first complement of ebx.
; get the first complement of dl.
; get the first complement of al.
; get the first complement of memory location. The size of the location
; must be specified.

h. Conditional jump instruction


ja dest -- Jump if above
jae dest -- Jump if above or equal
jb dest -- Jump if below
jbe dest -- Jump if below or equal
je dest -- Jump if equal
jne dest -- Jump if not equal
jmp dest -- Unconditional jump
iret -- Return from an interrupt

19

References:
[1] http://www.asmirvine.com/
[2] Intel Architecture Software Developers Manual Volume 1: Basic Architecture
[3] Guide to Assembly Language Programming in Linux book.
[4] http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/3rd-gen-coredesktop-vol-1-datasheet.pdf
[5] The art of assembly language book. http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/

20

Вам также может понравиться