Lecture 2: Input OUtput design

Download Report

Transcript Lecture 2: Input OUtput design

Microprocessor System Design
Omid Fatemi
Program writing and MASM
([email protected])
University of Tehran 1
Review
• Stack data structure
• Operands
• Addressing modes
• IO ports
University of Tehran 2
Outline
• Flag instruction
• ADD and ADC
• A loop program
• Data entering
• MASM
• Directives
University of Tehran 3
Flag Register
University of Tehran 4
Program
• Effect of ADD on carry flag
• Use of zero flag for looping
• Add 5 bytes in debug
• Using MASM
• Simple directives
• Consider carry
• Use ADC
University of Tehran 5
MASM 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.)
University of Tehran 6
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
University of Tehran 7
Anatomy of MASM Source File
• Assembler program divided into segments
• Segments defined in 1 or more modules
– contain instructions, data, assembler directives
• Each module is a separate file
– Assembler translates modules to object files
• Linker does several things
–
–
–
–
Combines multiple object files
Resolves relative addresses
Inserts loader code
Creates executable
University of Tehran 8
Assembler Language Segment Types
• Stack
– For dynamic data storage
– Source file defines size
– Must have exactly 1
• Data
–
–
–
–
For static data Storage
Source file defines size
Source file defines content (optional)
Can have 0 or more
• Code
– For machine Instructions
– Must have 1 or more
University of Tehran 9
Using MASM Assembler
• to get help:
C:\> masm /h
• Can just invoke MASM with no arguments:
C:\> masm
Source Filename
Object Filename
Source Listing
Cross Reference
[.ASM]:
hello
[HELLO.OBJ]:
[NUL.LST]:
[NUL.CRF]:
• .ASM - Assembler source file prepared by programmer
• .OBJ - Translated source file by assembler
• .LST - Listing file, documents “Translation” process
– Errors, Addresses, Symbols, etc
• .CRF – Cross reference file
University of Tehran 10
Using MASM Assembler/Linker (Cont.)
• Another way to invoke assembler:
C:\> masm hello,,hello,hello
• This causes MASM to create:
HELLO.OBJ
HELLO.LST
HELLO.CRF
• Yet another way:
C:\> masm hello
• This causes MASM to create:
HELLO.OBJ
• Next step is to create executable file using the linker:
C:\> link hello
• This causes linker to create:
HELLO.EXE
University of Tehran 11
MASM Assembler Language
• Each module contains 4 types of statements:
1.
2.
3.
4.
Executable instructions
MASM assembler directives
MASM macroinstruction definitions
MASM macroinstruction calls
Executable Instr.: Instructions that the x86 can fetch from memory and
execute
MASM Dir.: Programmer supplied directives that guide the “translation”
process
MASM Macro Defs. and Calls:
University of Tehran 12
x86 Instruction Type Classifications
• DATA TRANSFER
– General
mov
ax, [DAT1] ;ax gets contents of mem
• ARITHMETIC/LOGIC
–
–
–
–
Integer
Floating Point
Logical
Shifting
add
ax, bx
;ax gets ax+bx
fadd
DAT
;ST get ST+DAT
and
ax, bx
;ax gets ax AND bx
ror
ax, 2
;ax contents shifted-2 right
jnz
LABEL1
;if ZF=1 then IP=LABEL1
int
21h
;invoke INT handler 21h
• CONTROL TRANSFER
–
–
–
–
Branching
Interrupt
Subroutine call
JUMP
JMP
SUB1
LAbel
;invoke subroutine, SUB1
;CS:IP = label
University of Tehran 13
x86 Instruction Set Summary
(Not Included in Following Slides)
• Floating Point
8087
• Special Protected Mode
80386
• MMX (DSP)
Pentium MMX
• SSE (Streaming SIMD Extension)
- Special SIMD
Pentium III
• Others (few specialized added with each generation)
University of Tehran 14
MASM 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 University
programof entry
Tehran 15
Another Way to define Segments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Use 'assume' directive to define segment types
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT
;Define a segment
DB 100h DUP(?)
hellostk ENDS
hellodat
dos_print
strng
hellodat
SEGMENT
;define a segment
EQU 9
;define a constant
DB 'Hello World',13,10,'$' ;Define the character string
ENDS
hellocod
SEGMENT
;define a segment
assume cs:hellocod, ds:hellodat, ss: hellostk
mov ax, hellodat
;ax <-- data segment start address
mov ds, ax
;ds <-- initialize data segment register
mov ah, dos_print
;ah <-- 9 DOS 21h string function
mov dx,OFFSET strng
;dx <-- beginning of string
int 21h
;DOS service interrupt
mov ax, 4c00h
;ax <-- 4c DOS 21h program halt function
int 21h
;DOS service interrupt
ENDS
START
START:
hellocod
END
University of Tehran 16
Yet another way to define Segs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Use .stack,.data,.code directives to define segment types
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.stack 100h
; reserve 256 bytes of stack space
.data
dos_print EQU 9
strng
DB 'Hello World',13,10,'$'
;define a constant
;Define the character string
.code
START:
mov
mov
mov
mov
int
mov
int
ax, SEG strng
ds, ax
ah, dos_print
dx,OFFSET strng
21h
ax, 4c00h
21h
END
START
;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
University of Tehran 17
Masm 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
University of Tehran 18
Data Allocation 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)
University of Tehran 19
Home Work 2
Homework 2
Due: 19 Esphand 1381
1)
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
write a program in debug
calculate the sum of 4 words (each 16 bit data)
data are 1234H, 03FDH, 4FD3H and 11FFH
data is located at offset address of 0, 2, 4, 6
code starts at address 10H
the result should be stored in location 8,9
save your program including the data
run the program and write the sum.
In your email you should attach the file and write the sum value in your email.
The name of the file should be your firstname_lastname.com
Note that finding how to save the file in debug is your responsibility
2)
a.
b.
c.
d.
e.
f.
write an assembly program and the compile it using masm and link
move data from location of memory to another.
Source data is “Salam your_first_name”
Put separate code and data segment for your code
Put some spaces in your data segment for destination
Use masm and link and include .asm, .lst, .exe files in your email.
The name of the file should be hw2
University of Tehran 20
Summary
University of Tehran 21