cs 225 assemply lab manual -to be completed
TRANSCRIPT
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 1/20
1
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 ofproblems 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 theywill 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.
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 2/20
2
Course Plan:1. Basic Concepts
2. Assembly Language Fundamentals
3. Data Transfers, Addressing, and Arithmetic
4. Procedures
5.
Conditional Processing6. Integer Arithmetic
7. Strings and Arrays
8. 16-Bit MS-DOS Programming
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 3/20
3
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 TurboAssembler).
Coding with assembly language provides time-efficient and space-efficient codes. Assembly
language allows to access computer hardware easily. Assembly language composed assembleand 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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 4/20
4
general purpose registers[2]
Segment register[2]
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 5/20
5
Segment registers [2]
Data sizes [2]
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 6/20
6
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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 7/20
7
.625 2 = 1.25 1 (MSB)
.25 2 = 0.50 0
.5 2 = 1.0 1 (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
BinaryHexadicimal
BinaryHexadicimal
BinaryHexadicimal
BinaryHexadicimal
0000 0 0001 1 0010 2 0011 3
0100 1 0101 5 0110 6 0111 7
1000 2 1001 9 1010 A 1011 B
1100 C 1101 D 1110 E 1111 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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 8/20
8
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 (MSB)
0.625 x 16 = 10.0 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 Hexadicimal Binary Hexadicimal Binary Hexadicimal Binary Hexadicimal
000 0 001 1 010 2 011 3
100 1 101 5 110 6 111 7
201210 = 111110111002
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 9/20
9
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 (MSB)
0.03125 x 8 = 0.25 0
0.25 x 8 2.0 2 (LSB)
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 * 2
1 + 1 * 2
2 + 0 * 2
3 + 0 * 2
4 + 0 * 2
5 + 1 * 2
6 + 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 * 16
1 + 1 * 16
2 + A * 16
3 = 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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 10/20
10
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.
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 11/20
11
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 addingeach pair of bits together, the resulted carry is added with the next pair.
1 1 1 0 1 0 1 1 1 Carry
X 211 0 1 1 0 1 0 0 1 1 +
Y 343 1 0 1 0 1 0 1 1 1
554 0 0 0 1 0 1 0 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 1 0 1 0 1 1 1 Carry
( D3) 16 + 1 1 0 1 0 0 1 1 +
( 57) 16 0 1 0 1 0 1 1 1
( 12A) 16 0 0 1 0 1 0 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 1
st
complement, and ranges from -8 to +7 for2
nd complement.
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 12/20
12
Table 3 the code representation for signed and unsigned numbers
Signed number is represented either by one’s complement or by two’s 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
4-bit binary
value
Unsigned
decimal value
Signed decimal value
1st complement 2nd complement
0000 0 +0 0
0001 1 1 10010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 -7 -8
1001 9 -6 -7
1010 10 -5 -6
1011 11 -4 -5
1100 12 -3 -41101 13 -2 -3
1110 14 -1 -2
1111 15 -0 -1
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 13/20
13
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 two’s complemented, afterward; convertthe 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
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.
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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 14/20
14
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 (2
2) 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 between0 and (2
8-1) 255. The range of signed values using 2
nd 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
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?
Byte size Range of
Unsigned
decimal values
Range of Signed decimal values
1st complement 2nd complement
1 byte 0 to (2
8
-1) -(2
7
-1) to +(2
7
-1) -(2
7
) to +(2
7
-1)2 bytes 0 to (2
16-1) -(2
15-1) to +(2
15-1) -(2
15) to +(2
15-1)
4 bytes 0 to (232-1) -(231-1) to +(231-1) -(231) to +(231-1)
8 bytes 0 to (264-1) -(263-1) to +(263-1) -(263) to +(263-1)
10 bytes 0 to (280-1) -(279-1) to +(279-1) -(279) to +(279-1)
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 15/20
15
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.
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.
mov operand1, operand2 operand2 operand1
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:
CS
SS
DS
ES
EIP
EFLAGS
16-bit Segment Registers
EAX
EBX
ECX
EDX
32-bit General-Purpose Registers
FS
GS
EBP
ESP
ESI
EDI
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 16/20
16
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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 17/20
17
; 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
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 18/20
18
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.
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 19/20
19
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 ; get the first complement of ax.
not ebx ; get the first complement of ebx.
not dl ; get the first complement of dl.
not al ; get the first complement of al.
Not [mem] ; 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 jumpiret -- Return from an interrupt
7/23/2019 CS 225 Assemply Lab Manual -To Be Completed
http://slidepdf.com/reader/full/cs-225-assemply-lab-manual-to-be-completed 20/20
20
References:
[1] http://www.asmirvine.com/
[2] Intel Architecture Software Developer’s 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-core-
desktop-vol-1-datasheet.pdf
[5] The art of assembly language book. http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/