Upload
kalyan-tirunagari
View
213
Download
0
Embed Size (px)
Citation preview
8/4/2019 05-assembler1
1/20
DCSE CEG Anna University
Microprocessor System Design
Program writing and MASM
8/4/2019 05-assembler1
2/20
DCSE CEG Anna University
Review
Stack data structure
Operands
Addressing modes
IO ports
8/4/2019 05-assembler1
3/20
DCSE CEG Anna University
Outline
Flag instruction
ADD and ADC
A loop program
Data entering
MASM
Directives
8/4/2019 05-assembler1
4/20
DCSE CEG Anna University
Flag Register
8/4/2019 05-assembler1
5/20
DCSE CEG Anna University
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
8/4/2019 05-assembler1
6/20
DCSE CEG Anna University
MASM 8086 Instruction - Basic Structure
Label Operator Operand[s] ;Comment
Label- optional alphanumeric string1st character must be a-z,A-Z,?,@,_,$Last character must be :
Operator- assembly language instructionmnemonic: an instruction format for humansAssembler translates mnemonic into hexadecimal opcodeexample: mov is f8h
Operand[s]- 0 to 3 pieces of data required by instruction
Can be several different formsDelineated by commasimmediate, register name, memory data, memory address
Comment- Extremely useful in assembler language
These fields are separated by White Space (tab, blank, \n, etc.)
8/4/2019 05-assembler1
7/20
DCSE CEG Anna University
8086 Instruction - Example
Label Operator Operand[s] ;Comment
INIT: mov ax, bx ; Copy contents of bx into ax
Label - INIT:Operator - movOperands - ax andbxComment - alphanumeric string between ; and \n
Not case sensitive
Unlike other assemblers, destination operand is firstmov is the mnemonicthat the assembler translates into anopcode
8/4/2019 05-assembler1
8/20
DCSE CEG Anna University
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
8/4/2019 05-assembler1
9/20
DCSE CEG Anna University
AssemblerLanguage 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
8/4/2019 05-assembler1
10/20
DCSE CEG Anna University
Using MASM Assembler
to get help:
C:\> masm /h Can just invoke MASM with no arguments:
C:\> masm
Source Filename [.A SM]: hello
Object Filename [HELLO.OBJ]:
Source Listing [NUL.LST]:Cross Reference [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
8/4/2019 05-assembler1
11/20
DCSE CEG Anna University
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
8/4/2019 05-assembler1
12/20
DCSE CEG Anna University
MASM AssemblerLanguage
Each module contains 4 types of statements:
1. Executable instructions
2. MASM assembler directives
3. MASM macroinstruction definitions
4. MASM macroinstruction calls
Executable Instr.: Instructions that the x86 can fetch from memory andexecute
MASM Dir.: Programmer supplied directives that guide the translationprocess
MASM Macro Defs. and Calls:
8/4/2019 05-assembler1
13/20
DCSE CEG Anna University
x86 Instruction Type Classifications
DATA TRANSFER
Generalmov ax, [DAT1] ;ax gets contentsofmem
ARITHMETIC/LOGIC Integer add ax, bx ;ax getsax+bx
Floating Point fadd DAT ;ST get ST+DAT
Logical and ax, bx ;ax getsax AND bx
Shifting ror ax, 2 ;ax contentsshifted-2 right
CONTROL TRANSFER Branching jnz LABEL1 ;if ZF=1 then IP=LABEL1
Interrupt int 21h ;invoke INT handler 21h
Subroutinecall SUB1 ;invoke subroutine,SUB1
JUMP JMP LAbel ;CS:IP = label
8/4/2019 05-assembler1
14/20
DCSE CEG Anna University
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)
8/4/2019 05-assembler1
15/20
DCSE CEG Anna University
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 'ST ACK' ;Define the stack segment
DB 100h DUP(?) ;Set maximum stack size to 256 bytes (100h)hellostk ENDS
hellodat SEGMENT BYTE 'DATA' ;Define the data segmentdos_print EQU 9 ;define a constant via EQUstrng DB 'Hello World',13,10,'$' ;Define the character stringhellodat ENDS
hellocod SEGMENT BYTE 'CODE' ;Define the Code segmentSTART: mov ax, SEG hellodat ;ax
8/4/2019 05-assembler1
16/20
DCSE CEG Anna University
Another Way to define Segments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Use 'assume' directive to define segment types ;
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;hellostk SEGMENT ;Define a segment
DB 100h DUP(?)hellostk ENDS
hellodat SEGMENT ;define a segmentdos_print EQU 9 ;define a constant
strng DB 'Hello World',13,10,'$' ;Define the character stringhellodat ENDS
hellocod SEGMENT ;define a segmentassume cs:hellocod, ds:hellodat, ss: hellostk
START: mov ax, hellodat ;ax
8/4/2019 05-assembler1
17/20
DCSE CEG Anna University
Yet another way to define Segs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Use .stack,.data,.code directives to define segment types ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.stack 100h ; reserve 256 bytes of stack space
.datados_print EQU 9 ;define a constantstrng DB 'Hello World',13,10,'$' ;Define the character string
.code
START: mov ax, SEG strng ;ax
8/4/2019 05-assembler1
18/20
DCSE CEG Anna University
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
8/4/2019 05-assembler1
19/20
DCSE CEG Anna University
Data Allocation Directives
db define byte
dw define word (2 bytes)
dd define double word (4 bytes)
dq define quadword (8 bytes)
dt define tenbytesequ 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 equ 10 * 20 ; calculate a value (200)
8/4/2019 05-assembler1
20/20
DCSE CEG Anna University
List of Programs
1) write a program in debug
a. calculate the sum of 4 words (each 16 bit data)
b. data are 1234H, 03FDH, 4FD3H and 11FFH
c. data is located at offset address of 0, 2, 4, 6
d. code starts at address 10H
e. the result should be stored in location 8,9
f. save your program including the data
g. run the program and write the sum.
h. In your email you should attach the file and write the sum value in your email.
i. The name of the file should be your firstname_lastname.com
j. Note that finding how to save the file in debug is your responsibility
2) write an assembly program and the compile it using masm and link
a. move data from location of memory to another.
b. Source data is Salam your_first_name
c. Put separate code and data segment for your code
d. Put some spaces in your data segment for destination
e. Use masm and link and include .asm, .lst, .exe files in your email.
f. The name of the file should be hw2