lecture6
Post on 06-Aug-2015
47 Views
Preview:
TRANSCRIPT
Lecture 06
Programming The 8086 In
AAiT
Programming The 8086 In
Assembly Language…Continued
Daniel D.Daniel D.Daniel D.Daniel D. DECEECEg ECEg ECEg ECEg ---- 4501450145014501
� Assembler directives & operators
IssuesAAiT
� program examples & exercises
1ECEg ECEg ECEg ECEg ---- 4501450145014501
Assembler directives (Preprocessors)
ASSUME tell the assembler the name of the logical segment it
should use for a specified segment.
e.g. ASUME CS: code
DB (define byte) Used to declare byte type variable(s) and set aside
storage for it
e.g. High_scores DB 82H, 78H, 75H
AAiT
e.g. High_scores DB 82H, 78H, 75H
Name DB “ZELALEM”
Also, DW, DD, DQ, DT
END end of program. Assembler ignores any instruction after END
EQU (equate) Used to give a name to some value or symbol.
e.g. Ratio EQU 05h
Henceforth, Ratio can be used instead of 05h in the program
2ECEg ECEg ECEg ECEg ---- 4501450145014501
Processor directives
EVEN tell the assembler to align the data or instruction to the even
bank.
EXTRN Used to tell the assembler that the names or labels following
the directive are in some other assembly module.
GLOBAL can be used in place of PUBLIC or EXTRN directives. It is used
to make the symbol available to other modules.
INCLUDE Include Source Code from File
AAiT
INCLUDE Include Source Code from File
e.g. INCLUDE “program_2.asm”
LENGTH Is an operator, which tells the assembler to determine the
number of elements in some named data item, such as a
string or an array.
e.g. MOV CX, LENGTH string1 CX = no of elements in string1
3ECEg ECEg ECEg ECEg ---- 4501450145014501
Processor directives
NAME Used to give a specific name to each assembly module
when programs consisting of several modules are written.
e.g. NAME “Main”
OFFSET Is an operator which tells the assembler to determine the
offset or the displacement of a named data item (variable) or
procedure from start of the segment which contains it.
e.g. MOV BX, OFFSET TABLE
AAiT
e.g. MOV BX, OFFSET TABLE
ORG allows to set the location counter to a desired value at any
point in the program.
e.g. ORG 100H set the location counter to 0100H
ORG $ +100 Reserve the next 100 bytes
NB: Refer to the handout for other directives
4ECEg ECEg ECEg ECEg ---- 4501450145014501
Program Examples
E.g. 1
Determine the contents of the GP registers and flags after these
programs.
MOV AX,1234H V DB 5, 8, 12, 1
MOV BX,5678H MOV AL, 0
PUSH AX MOV BX, 0
PUSH BX MOV DL, 24
ORG 40H
JMP START
NAME DB “Abebe”
START:
MOV SI, OFFSET NAME
AAiT
PUSH BX MOV DL, 24
POP AX DO: ADD AL, V[BX]
POP BX INC BX
CMP AL, DL
JLE DO
AX ? AL ? DL ? BX ?
BX ? ZF ? CF ? PF ? IP? AL? CX? [SI]? ZF?
MOV SI, OFFSET NAME
INC SI
LOADS [SI]
MOV CX, 05H
REPEAT: INC SI
CMP AL, [SI]
LOOPE REPEAT
5ECEg ECEg ECEg ECEg ---- 4501450145014501
Program Examples
E.g. 2
1. Write a program that takes temperature values in Celsius
from the keyboard; changes to Fahrenheit; and displays
the result on the screen, like:
20 C = 68 F
or 05 C = 41 F
AAiT
or 05 C = 41 F
�take input range 00-99 c
�Use INT 21h/ah = 1……………for input function
� Use INT 21h/ah = 2……………for character display
�TF = 9/5 Tc + 32
6ECEg ECEg ECEg ECEg ---- 4501450145014501
E.g. 2 solution
The input sub-program
name “input”
org 100h ;locate code starting here
jmp start
input db 0,0 ;reserve memory for i/p
AAiT
input db 0,0 ;reserve memory for i/p
output db 0,0,0 ;and for o/p
;take a two digit no and save @input
start:
lea bx, input ;points to 1st input variable
mov si, bx ;keep the input address
mov cx, 2 ;no of chars.
mov ah, 1 ;for input function
INP:
int 21h ;input function
mov [bx], al ;save the i/p
inc bx
loop INP
Next slide
7ECEg ECEg ECEg ECEg ---- 4501450145014501
E.g. 2 solution….cnt’d
The conversion sub-program
name “conversion”
;form a single no from the inputs
mov dl, 10
mov al, [si] ;fist digit
and al, 0Fh ;change to decimal
;convert to Fahrenheit
mov dl, 5
div dl ;divide by 5
AAiT
and al, 0Fh ;change to decimal
mul dl ;10’s decimal place
inc si
mov dl, [si] ;second digit
and dl, 0Fh ;change to decimal
add al, dl ;one’s decimal place
div dl ;divide by 5
mov dl, 9
mul dl ;multiply by 9
add al, 32 ;add 32
Next slide
8ECEg ECEg ECEg ECEg ---- 4501450145014501
E.g. 2 solution….cnt’d
The formatting sub-program (to ASCII)
name “format”
; change result to ASCII & store @output
mov cx, 3
lea bx, output ;point in memory @output
add bx, 2 ;start at one’s decimal place
mov di, bx ;keep the o/p address
ascii:
mov ah, 0
cmp al, 10
jb done
div dl ;ax/10: q=al, r=ah
or ah, 30h ;convert to ASCII
AAiT
mov di, bx ;keep the o/p address
mov dl, 10
or ah, 30h ;convert to ASCII
mov [bx], ah
dec bx
loop ascii
done: or al, 30h ;one’s place
mov [bx], al
NB: Character input and output is implemented in ASCII format,
that’s why we need to convert ASCII to decimal at input and
decimal to ASCII at output.
Next slide
9ECEg ECEg ECEg ECEg ---- 4501450145014501
E.g. 2 solution….cnt’d
The display sub-program
; display the result in the
; required format
name “display”
mov cx, 2
mov ah, 2
lea si, input
mov cx,3 ;display output
op: cmp bx, di
ja finish
mov dl,[bx]
AAiT
lea si, input
ip: mov dl, [si] ;display input
int 21h
inc si
loop ip
mov dl, 'C' ;display C
int 21h
mov dl, ' = ' ;display =
int 21h
mov dl,[bx]
int 21h
inc bx
loop op
finish: mov dl,'F‘ ;display F
int 21h
ret ;return to operating system
10ECEg ECEg ECEg ECEg ---- 4501450145014501
Exercise:
2. Consider a login system. Suppose you have a 5-character
password stored in DS in memory. Write a program that
reads a 5-char password string from the keyboard port;
compare the string with the password in memory; prints
“access granted” if correct or “access denied” otherwise.
AAiT
“access granted” if correct or “access denied” otherwise.
�Use INT 21h/ah = 1…………for input function
� Use INT 21h/ah = 9…………to display a string
�Use CMPSB to compare strings
11ECEg ECEg ECEg ECEg ---- 4501450145014501
top related