Examples - WordPress.com
Download
Report
Transcript Examples - WordPress.com
Computer and Information Sciences College /
Computer Science Department
CS 206 D
Computer Organization
and Assembly Language
Data Representation
Binary 0-1
represents the state of
electronic components
used in computer systems
Bit - Binary digit
Byte - 8 Bits
smallest addressable
memory location (on the
IBM-PC)
Word - 16 Bits
Each architecture
may define its own
“wordsize”
Doubleword - 32 Bits
Quadword - 64 Bits
Nybble - 4 Bits
DB
8
DW 16
bits
DD 32 bits
DQ 64 bits
Defining Types of data – Array byte
• an array is a sequence of memory bytes or words.
• Example:
B_ARRAY DB 10H,20H,30H
W_ARRAY DW 1000,40,29887,329
Symbol
Address
Contents
B_ARRAY
B_ARRAY+1
B_ARRAY+2
200H
201H
202H
10H
20H
30H
W_ARRAY
W_ARRAY+2
W_ARRAY+4
W_ARRAY+6
203H
205H
207H
209H
1000D
40D
29887D
329D
Addresses with Displacements
b db 4Fh, 20h, 3Ch
w dw 2048, -100, 0
mov
mov
mov
mov
bx, w+2
b+1, ah
ah, b+5
dx, w-3
The assembler
computes an address
based on the
expression
NOTE: These are
address computations
done at assembly time
MOV ax, b-1
will not subtract 1 from
the value stored at b
Numeric Constant
• In an assembly language program, we may express data as:
• Binary: bit string followed by ‘B’ or ‘b’
• Decimal: string of decimal digits followed by an optional ‘D’ or
‘d’
• Hex: begins with a decimal digit and ends with ‘H’ or ‘h’
• Real : end with ‘R’ and the assembler converts a given a
decimal or hex constant to floating point number
• Any number may have an optional sign.
•The decimal range is:
• Unsigned representation: 0 to 255
• Signed representation: -128 to 127
5
Numeric Constant
Number
Type
11011
1101B
64223
-21843D
1,234
1B4DH
1B4D
FFFFH
0FFFFH
decimal
binary
decimal
decimal
illegal
hex
illegal
illegal
hex
6
Character String
• ASCII codes can be initialized with a string of
characters using single quotes like ‘PC’ or double quotes like “PC”.
• Example:
LETTERS DB
=
LETTERS DB
'ABC'
41H,42H,43H
• Inside a string, the assembler differentiates between upper and lower
case.
• It is possible to combine characters and numbers in one definition:
Example: MSG
DB
'HELLO',0AH,0DH, '$'
7
Named Constants - EQU (Equates)
• To assign a name to a constant, we can use the EQU pseudo-op.
• Syntax:
name EQU constant
• Examples:
LF
EQU 0AH
MOV DL,LF = MOV DL,0AH
PROMPT EQU 'Any Thing'
MSG DB 'Any Thing' = MSG DB PROMPT
• Note: no memory is allocated for EQU names.
8
Assembly Language Instructions
Mnemonics represent Machine Instructions
Each mnemonic used represents a single
machine instruction
The assembler performs the translation
Some mnemonics require operands
Operands provide additional information
register, constant, address, or variable
Assembler Directives
8086 Instruction - Basic Structure
Label
Operator
Operand[s]
;Comment
Label - optional alphanumeric string
1st character must be a-z, A-Z,?,@,_,$
Last character must be :
Operator - assembly language instruction
mnemonic: an instruction format for humans
Assembler translates mnemonic into hexadecimal opcode
example: mov is f8h
Operand[s] - 0 to 3 pieces of data required by instruction
Can be several different forms
Delineated by commas
immediate, register name, memory data, memory address
Comment - Extremely useful in assembler language
These fields are separated by White Space (tab, blank, \n, etc.)
8086 Instruction - Example
Label
Operator
INIT:mov
Label
Operator
Operands
Comment
ax, bx
-
Operand[s] ;Comment
; Copy contents of bx into
ax
INIT:
mov
ax and bx
alphanumeric string between ; and \n
• Not case sensitive
• Unlike other assemblers, destination operand is first
• mov is the mnemonic that the assembler translates into an
opcode
Instruction Set of 8086
•Data moving instructions.
–Data can be moved from register to register, register to
memory and memory to register.
•Arithmetic - add, subtract, increment, decrement,
convert byte/word and compare.
•Logic - AND, OR, exclusive OR, shift/rotate and test.
•String manipulation - load, store, move, compare and
scan for byte/word.
•Control transfer - conditional, unconditional, call
subroutine and return from subroutine.
•Input/Output instructions.
•Other - setting/clearing flag bits, stack operations,
software interrupts, etc
Lecture 2 :
x86 Instruction Set Summary
(Data Transfer)
CBW
CWD
IN
LAHF
LDS
LEA
LES
LODS
MOV
MOVS
OUT
POP
POPF
PUSH
PUSHF
SAHF
STOS
XCHG
XLAT
;Convert Byte to Word AL AX
;Convert Word to Double in AX DX,AX
;Input
;Load AH from Flags
;Load pointer to DS
;Load EA to register
;Load pointer to ES
;Load memory at SI into AX
;Move
;Move memory at SI to DI
;Output
;Pop
;Pop Flags
;Push
;Push Flags
;Store AH into Flags
;Store AX into memory at DI
;Exchange
;Translate byte to AL
x86 Instruction Set Summary
(Arithmetic/Logical)
AAA
AAD
AAM
AAS
ADC
ADD
AND
CMC
CMP
CMPS
DAA
DAS
DEC
DIV
IDIV
MUL
IMUL
INC
;ASCII Adjust for Add in AX
;ASCII Adjust for Divide in AX
;ASCII Adjust for Multiply in AX
;ASCII Adjust for Subtract in AX
;Add with Carry
;Add
;Logical AND
;Complement Carry
;Compare
;Compare memory at SI and DI
;Decimal Adjust for Add in AX
;Decimal Adjust for Subtract in AX
;Decrement
;Divide (unsigned) in AX(,DX)
;Divide (signed) in AX(,DX)
;Multiply (unsigned) in AX(,DX)
;Multiply (signed) in AX(,DX)
;Increment
x86 Instruction Set Summary
(Arithmetic/Logical Cont.)
NEG
NOT
OR
RCL
RCR
ROL
ROR
SAR
SBB
SCAS
SHL/SAL
SHR
SUB
TEST
XLAT
XOR
;Negate
;Logical NOT
;Logical inclusive OR
;Rotate through Carry Left
;Rotate through Carry Right
;Rotate Left
;Rotate Right
;Shift Arithmetic Right
;Subtract with Borrow
;Scan memory at DI compared to AX
;Shift logical/Arithmetic Left
;Shift logical Right
;Subtract
;AND function to flags
;Translate byte to AL
;Logical Exclusive OR
x86 Instruction Set Summary
(Control/Branch Cont.)
CALL
CLC
CLD
CLI
ESC
HLT
INT
INTO
IRET
JB/JNAE
JBE/JNA
JCXZ
JE/JZ
JL/JNGE
JLE/JNG
JMP
JNB/JAE
JNBE/JA
JNE/JNZ
JNL/JGE
;Call
;Clear Carry
;Clear Direction
;Clear Interrupt
;Escape (to external device)
;Halt
;Interrupt
;Interrupt on Overflow
;Interrupt Return
;Jump on Below/Not Above or Equal
;Jump on Below or Equal/Not Above
;Jump on CX Zero
;Jump on Equal/Zero
;Jump on Less/Not Greater or Equal
;Jump on Less or Equal/Not Greater
;Unconditional Jump
;Jump on Not Below/Above or Equal
;Jump on Not Below or Equal/Above
;Jump on Not Equal/Not Zero
;Jump on Not Less/Greater or Equal
Assembler Directives
end label
end of program, label is entry point
proc far|near
begin a procedure; far, near keywords
specify if procedure in different code
segment (far), or same code segment (near)
endp
end of procedure
page
set a page format for the listing file
title
title of the listing file
.code
mark start of code segment
.data
mark start of data segment
.stack
set size of stack segment
x86 Instruction Set Summary
(Control/Branch)
JNLE/JG
JNO
JNP/JPO
JNS
JO
JP/JPE
JS
LOCK
LOOP
LOOPNZ/LOOPNE
LOOPZ/LOOPE
NOP
REP/REPNE/REPNZ
REPE/REPZ
RET
SEG
STC
STD
STI
TEST
WAIT
;Jump on Not Less or Equal/Greater
;Jump on Not Overflow
;Jump on Not Parity/Parity Odd
;Jump on Not Sign
;Jump on Overflow
;Jump on Parity/Parity Even
;Jump on Sign
;Bus Lock prefix
;Loop CX times
;Loop while Not Zero/Not Equal
;Loop while Zero/Equal
;No Operation (= XCHG AX,AX)
;Repeat/Repeat Not Equal/Not Zero
;Repeat Equal/Zero
;Return from call
;Segment register
;Set Carry
;Set Direction
;Set Interrupt
;AND function to flags
;Wait
Assembler Directives
db
define byte
dw
define word (2 bytes)
dd
define double word (4 bytes)
dq
define quadword (8 bytes)
dt
define tenbytes
equ
equate, assign numeric expression to a name
Examples:
db 100 dup (?)
define 100 bytes, with no initial values for bytes
db “Hello”
define 5 bytes, ASCII equivalent of “Hello”.
maxint equ
32767
count
10 * 20
equ
; calculate a value (200)
Program Example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;
This is an example program. It prints the
;
;
character string "Hello World" to the DOS standard output
;
;
using the DOS service interrupt, function 9.
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT BYTE STACK 'STACK'
;Define the stack segment
DB 100h DUP(?)
;Set maximum stack size to 256 bytes (100h)
hellostk ENDS
hellodat
dos_print
strng
hellodat
SEGMENT BYTE 'DATA' ;Define the data segment
EQU 9
;define a constant via EQU
DB 'Hello World',13,10,'$' ;Define the character string
ENDS
hellocod
START:
SEGMENT BYTE 'CODE' ;Define
mov ax, SEG hellodat
mov ds, ax
mov ah, dos_print
mov dx,OFFSET strng
int 21h
mov ax, 4c00h
int 21h
ENDS
END
START
hellocod
the Code segment
;ax <-- data segment start address
;ds <-- initialize data segment register
;ah <-- 9 DOS 21h string function
;dx <-- beginning of string
;DOS service interrupt
;ax <-- 4c DOS 21h program halt function
;DOS service interrupt
; ‘END label’ defines program entry
Creating and Running a Program
Editor
A text editor or word processor is used to create a
source program file.
.ASM file
Assembler
.OBJ file
Linker
.EXE file
An assembler is used to translate the source file
into a machine language object file.
Assembling: translate source program (written in
assembly language) into machine code (object
code)
A linker is used to link one or more object files
to create a run file.
Linking: complete machine code for the object
program, generate an executable module
Step 2: Assembling & Linking the program
• After printing the copyright information, the assembler will check
the source file for syntax errors:
• If one or more errors were found:
• The assembler will display the line number of each error and
a short description.
• If no errors were found:
• The assembler will translate the assembly language code into
the assembly machine language object file (.obj file).
• The linker will take one or more object files, fills any
missing addresses, and combines the object files into a
single executable file (.exe file).