Академический Документы
Профессиональный Документы
Культура Документы
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
Segment register[2]
.625 2 = 1.25
.25 2
= 0.50
.5 2
= 1.0
(MSB)
(LSB)
0.62510 = 0.1012
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
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:
= 0.03125
(MSB)
x 8 = 0.25
x 8
2 (LSB)
2.0
0.039062510 = .00216
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.
10
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
Unsigned
decimal value
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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)
-(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.
EBP
EBX
ESP
ECX
ESI
EDX
EDI
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 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
; 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
; 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
; 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
; 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
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
18
operand1 = first-complement(operand1)
not reg8,16,32
not memory8,16,32
Ex:
not ax
not ebx
not dl
not al
Not [mem]
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