Programming The 8086 In Assembly Language Continued

AAiT Lecture 06 Programming The 8086 In Assembly Language …Continued ECEg - 4501 Daniel D. DECE Issues AAiT  Assembler directives & operators ...
40 downloads 0 Views 1014KB Size
AAiT

Lecture 06

Programming The 8086 In Assembly Language …Continued

ECEg - 4501

Daniel D. DECE

Issues AAiT

 Assembler directives & operators  program examples & exercises

ECEg - 4501

1

Assembler directives (Preprocessors) AAiT

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 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 ECEg - 4501

2

Processor directives

AAiT

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 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 EVEN

ECEg - 4501

3

Processor directives AAiT

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 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

ECEg - 4501

4

Program Examples

AAiT

E.g. 1 Determine the contents of the GP registers and flags after these programs. ORG 40H MOV AX,1234H V DB 5, 8, 12, 1 JMP START MOV BX,5678H MOV AL, 0 NAME DB “Abebe” PUSH AX MOV BX, 0 START: PUSH BX MOV DL, 24 MOV SI, OFFSET NAME POP AX DO: ADD AL, V[BX] INC SI INC BX POP BX LOADS [SI] CMP AL, DL MOV CX, 05H JLE DO REPEAT: INC SI AX ? BX ? ECEg - 4501

AL ? DL ? BX ? ZF ? CF ? PF ?

CMP AL, [SI] LOOPE REPEAT IP? AL? CX? [SI]? ZF? 5

Program Examples

AAiT

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 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 ECEg - 4501

6

E.g. 2 solution

AAiT

The input sub-program name “input” org 100h ;locate code starting here jmp start 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

ECEg - 4501

INP: int 21h ;input function mov [bx], al ;save the i/p inc bx loop INP Next slide

7

E.g. 2 solution….cnt’d

AAiT

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 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

;convert to Fahrenheit mov dl, 5 div dl ;divide by 5 mov dl, 9 mul dl ;multiply by 9 add al, 32 ;add 32 Next slide

ECEg - 4501

8

E.g. 2 solution….cnt’d

AAiT

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 mov dl, 10

ascii: mov ah, 0 cmp al, 10 jb done div dl ;ax/10: q=al, r=ah or ah, 30h ;convert to ASCII mov [bx], ah dec bx loop ascii done: or al, 30h ;one’s place mov [bx], al Next slide

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. ECEg - 4501

9

E.g. 2 solution….cnt’d

AAiT

The display sub-program ; display the result in the ; required format name “display” mov cx, 2 mov ah, 2 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 ECEg - 4501

mov cx,3 ;display output op: cmp bx, di ja finish mov dl,[bx] int 21h inc bx loop op finish: mov dl,'F‘ ;display F int 21h ret ;return to operating system

10

Exercise:

AAiT

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. Use INT 21h/ah = 1…………for input function  Use INT 21h/ah = 9…………to display a string Use CMPSB to compare strings ECEg - 4501

11