Chapter1c
-
date post
21-Oct-2014 -
Category
Education
-
view
371 -
download
0
description
Transcript of Chapter1c
![Page 1: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/1.jpg)
I. INTRODUCTION
Developing Assembly Language
Programs
![Page 2: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/2.jpg)
Instructions and Directives
Instructions
tell processor what to do
assembled into machine code by
assembler
executed at runtime by the processor
from the Intel x86 instruction set
![Page 3: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/3.jpg)
Instructions and Directives
Directives
tell assembler what to do
commands that are recognized and
acted upon by the assembler
not part of the instruction set
used to declare code and data areas,
define constants and memory for storage
different assemblers have different
directives
![Page 4: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/4.jpg)
Assembler Directives
EQU directive
defines constants
– label equ value
– count equ 100
Data definition directive
defines memory for data storage
defines size of data
![Page 5: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/5.jpg)
Assembler Directives
Initialized Data
db – define byte
dw – define word
dd – define double
– label directive initial value
– int db 0
– num dw 100
![Page 6: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/6.jpg)
Assembler Directives
Character constants
single quote delimited
‘A’
– char db ‘!’
![Page 7: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/7.jpg)
Assembler Directives
String constants
single quote delimited or sequence of
characters separated by commas
each character is one byte each
‘hello’
‘h’, ’e’, ’l’, ’l’, ’o’
– prompt1 db ‘Please enter number: ’
– prompt2 db ‘Please enter number: ’,10
![Page 8: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/8.jpg)
Assembler Directives
Declarations
![Page 9: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/9.jpg)
Assembler Directives
Uninitialized Data
resb – reserve byte
resw – reserve word
resd – reserve double word
– label directive value
– num resb 1 ; reserves 1 byte
– nums resb 10 ; reserves 10 bytes
![Page 10: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/10.jpg)
Assembler Directives
Uninitialized
variables
![Page 11: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/11.jpg)
Assembly Instructions
Basic Format
instruction operand1, operand2
Operand
Register
Immediate
Memory
![Page 12: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/12.jpg)
Instruction Operands
Registers
eax, ax, ah, al
ebx, bx, bh, bl
ecx, cx, ch, cl
edx, dx, dh, dl
![Page 13: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/13.jpg)
Instruction Operands
Immediate
character constants
– character symbols enclosed in quotes
– character ASCII code
integer constants
– begin with a number
– ends with base modifier (B, O or H)
‘A’ = 65 = 41H = 01000001B
![Page 14: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/14.jpg)
Instruction Operands
Memory
when using the value of a variable,
enclose the variable name in square
brackets
[num] - value of num
num - address of num
![Page 15: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/15.jpg)
Instruction Operands
If the operands are registers or
memory locations, they must be of
the same type.
Two memory operands are not
allowed in an instruction.
![Page 16: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/16.jpg)
LABORATORY TOPICS
Basic Input/Output
![Page 17: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/17.jpg)
Basic Input/Output
Interrupts
can be a hardware or software interrupt
the printer is out of paper
– hardware interrupt
assembly program issuing a system call
– software interrupt
An interrupt simply gets the attention of the processor so that it would context switch and execute the system's interrupt handler being invoked.
![Page 18: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/18.jpg)
Basic Input/Output
Interrupts
break the program execution cycle
(Fetch-Decode-Execute)
wait for interrupt to be serviced
Linux services
int 0x80, int 80H – function calls
![Page 19: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/19.jpg)
INT 80H
system call numbers for this service are
placed in EAX
1 – sys_exit (system exit)
3 – sys_read (read from standard input)
4 – sys_write (write to standard output)
![Page 20: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/20.jpg)
INT 80H
mov eax, 1
mov ebx, 0
int 80H
terminate program
return 0 at the end of main program (no
error)
![Page 21: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/21.jpg)
INT 80H
mov eax, 3
mov ebx, 0
mov ecx, <address>
int 80H
reads user input
stores input to a variable referenced by address
![Page 22: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/22.jpg)
INT 80H
mov eax, 4
mov ebx, 1
mov ecx, <string>
mov edx, <length>
int 80H
outputs a character/string
character/string must be in ECX and string
length must be in EDX before issuing interrupt
![Page 23: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/23.jpg)
Basic Input/Output
Instructions
![Page 24: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/24.jpg)
Basic Input/Output
![Page 25: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/25.jpg)
Basic Input/Output
![Page 26: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/26.jpg)
Basic Input/Output
![Page 27: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/27.jpg)
Basic Input/Output
![Page 28: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/28.jpg)
Basic Input/Output
![Page 29: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/29.jpg)
Converting Characters to Numbers
Input read from the keyboard are ASCII-
coded characters.
Output displayed on screen are ASCII-
coded characters.
To perform arithmetic, a numeric character
must be converted from ASCII to its
equivalent value.
To print an integer, a number must be
converted to its equivalent ASCII
character(s).
![Page 30: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/30.jpg)
Converting Characters to Numbers
Character to Number:
section .bss
num resb 1
section .text
mov eax, 3
mov ebx, 0
mov ecx, num
int 80h
![Page 31: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/31.jpg)
Converting Characters to Numbers
Character to Number:
section .bss
num resb 1
section .text
mov eax, 3
mov ebx, 0
mov ecx, num
int 80h
sub [num], 30h
![Page 32: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/32.jpg)
Converting Numbers to Characters
Number to Character:
section .text
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 1
int 80h
![Page 33: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/33.jpg)
Converting Numbers to Characters
Number to Character:
section .text
add [num], 30h
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 1
int 80h
![Page 34: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/34.jpg)
ASCII Table
![Page 35: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/35.jpg)
ASCII Table
![Page 36: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/36.jpg)
Converting Characters to Numbers
Character to Number:
mov ecx, num
int 80h
sub [num], 30h
num =
30h =
num =
0 0 1 1 0 1 0 0
0 0 1 1 0 0 0 0
0 0 0 0 0 1 0 0
![Page 37: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/37.jpg)
Converting Numbers to Characters
Number to Character:
add [num], 30h
num =
30h =
num =
0 0 0 0 0 1 1 0
0 0 1 1 0 0 0 0
0 0 1 1 0 1 1 0
![Page 38: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/38.jpg)
LABORATORY TOPICS
Implementing Sequential
Statements
![Page 39: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/39.jpg)
Assignment Instruction
mov instruction
mov destination, source
mov is a storage command
copies a value into a location
destination may be register or memory
source may be register, memory or
immediate
operands should not be both memory
operands must be of the same size
![Page 40: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/40.jpg)
Simple Instructions
inc destination
– increase destination by 1
dec destination
– decrease destination by 1
neg destination
– negate destination (2’s complement)
destination may be a register or a memory
![Page 41: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/41.jpg)
Add and Sub Instructions
add destination, source
destination = destination + source
sub destination, source
destination = destination – source
same restrictions as the mov instruction
![Page 42: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/42.jpg)
Multiplication
mul source
Source is the multiplier
Source may be a register or memory
If source is byte-size then
AX = AL * source
If source is word-size thenDX:AX = AX * source
If source is double word-size then
EDX:EAX = EAX * source
![Page 43: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/43.jpg)
Multiplication
mov al, 2
mov [num], 80H
mul byte [num]
![Page 44: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/44.jpg)
Multiplication
mov al, 2
mov [num], 80H
mul byte [num]
This results in AX = 2 * 128 = 256 = 0100 H
![Page 45: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/45.jpg)
Multiplication
mov al, 2
mov [num], 80H
mul byte [num]
This results in AX = 2 * 128 = 256 = 0100 H
In Binary, AX = 0000000100000000 B
![Page 46: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/46.jpg)
Multiplication
mov al, 2
mov [num], 80H
mul byte [num]
This results in AX = 2 * 128 = 256 = 0100 H
In Binary, AX = 0000000100000000 B
So, AH = 1 and AL = 0.
![Page 47: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/47.jpg)
Multiplication
mov ax, 2mov [num], 65535mul word [num]
This results in 2 * 65535 = 131070
= 0001FFFE H.
DX = 1 and AX = FFFE H
Only when taken together will one get the
correct product.
![Page 48: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/48.jpg)
Division
div source
Source is the divisor
Source may be a register or memory
If source is byte-size then
AH = AX mod source
AL = AX div source
![Page 49: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/49.jpg)
Division
If source is word-size then
DX = DX:AX mod source
AX = DX:AX div source
If source is double word-size then
EDX = EDX:EAX mod source
EAX = EDX:EAX div source
div == integer division
mod == modulus operation (remainder)
![Page 50: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/50.jpg)
Division (Divide word by byte)
mov ax, 257
mov [num], 2
div byte [num]
![Page 51: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/51.jpg)
Division (Divide word by byte)
mov ax, 257
mov [num], 2
div byte [num]
This results in AH = 1 and AL = 128
![Page 52: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/52.jpg)
Division (Divide byte by byte)
mov [num], 129
mov al, [num]
mov ah, 0
mov [num], 2
div byte [num]
![Page 53: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/53.jpg)
Division (Divide byte by byte)
mov [num], 129
mov al, [num]
mov ah, 0
mov [num], 2
div byte [num]
AX = 129 since AH = 0 and AL = 129
![Page 54: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/54.jpg)
Division (Divide byte by byte)
mov [num], 129
mov al, [num]
mov ah, 0
mov [num], 2
div byte [num]
AX = 129 since AH = 0 and AL = 129
This results in AH = 1 and AL = 64
![Page 55: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/55.jpg)
Division (Divide word by word)
mov dx, 0
mov ax, 65535
mov [num], 32767
div word [num]
DX:AX = 65535
num = 32767
This results in DX = 1 and AX = 2.
![Page 56: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/56.jpg)
Division (Divide word by word)
mov dx, 1
mov ax, 65535
mov [num], 65535
div word [num]
Taken together the values in DX and AX is 131071.
This is divided by 65535.
The results are in DX = 1 and in AX = 2.
![Page 57: Chapter1c](https://reader033.fdocuments.net/reader033/viewer/2022051322/5446e937b1af9fe33a8b46f7/html5/thumbnails/57.jpg)
Divide Overflow Error
mov ax, 65535
mov [num], 2
div byte [num]
Dividing 65535 by 2 results in a quotient of
32767 with a remainder of 1.
The value 32767 will not fit in AL, the destination
of the quotient, thus resulting in an error.