Chapter 4.2 Introduction To Assembly Language
Chapter 4.2 Introduction To Assembly Language
Chapter 4.2 Introduction To Assembly Language
CHAPTER 4.2
ASSEMBLY LANGUAGE PROGRAMMING
INPUT/OUTPUT - CONT.
INT 21H used to invoke a large number of
DOS function.
Type of called function specified by putting a
number in AH register.
AH=1 single-key input with echo
AH=2 single-character output
AH=9 character string output
AH=8 single-key input without echo
AH=0Ah character string input
INPUTTING AND DISPLAYING
CHARACTER
3
DISPLAYING A STRING
Input: AH=9, DX= offset address of a string.
String must end with a ‘$’ character.
To display the message Hello!
MSG DB “Hello!$”
MOV AH, 9
MOV DX, offset MSG
INT 21H
OFFSET operator returns the address of a
variable
The instruction LEA (load effective address) loads
destination with address of source
LEA DX, MSG
INPUTTING A STRING
Input: AH=10, DX= offset address of a buffer to store
read string.
First
byte of buffer should contain maximum string size+1
Second byte of buffer reserved for storing size of read string.
To read a Name of maximum size of 20B display it
Name DB 21,0,20 dup(“$”)
MOV AH, 10
LEA DX, Name
Mov ah ,
INT 21H
MOV AH, 9
LEA DX, Name+2
INT 21H
INPUT AND OUTPUT STRING
.model small
.stack 100
.data
array db 21,?,20 dup('$')
\n db 10,13,'$'
.code
start:
mov ax,@data
mov ds,ax
mov ah,10
lea dx,array
int 21h
mov dx,offset \n
mov ah,9h
int 21h
mov dx,offset array+2
mov ah,9h
int 21h
end start
6
FINDING THE OCCURRENCE OF
NUMBER IN AN ARRAY
.model small
.stack 100
.data
array db 63h,32h,45h,75h,12h,42h,09h,14h,56h,38
search db 09h
posi db ?
.code
start:
mov ax,@data
mov ds,ax
mov es,ax
mov cx,000ah
lea di,array
mov al,search
cld
repne scans array
mov al,10
sub al,cl
end start
7
A CASE CONVERSION PROGRAM
Prompt the user to enter a lowercase letter,
and on next line displays another message
with letter in uppercase.
Enter a lowercase letter: a
In upper case it is: A
.DATA
CR EQU 0DH
LF EQU 0AH
MSG1 DB ‘Enter a lower case letter: $’
MSG2 DB CR, LF, ‘In upper case it is: ‘
Char DB ?, ‘$’
A CASE CONVERSION PROGRAM -
CONT.
.CODE
Start: ; initialize data segment
LEA DX, MSG1; display first message
MOV AH, 9
INT 21H
MOV AH, 1 ; read character
INT 21H
SUB AL, 20H ; convert it to upper case
MOV CHAR, AL ; and store it
LEA DX, MSG2; display second message and
MOV AH, 9 ; uppercase letter
INT 21h
Mov ax, 4c00h
Int21
End start ; return to DOS
COPYING A STRING TO
ANOTHER
.DATA
String1 DB “Hello”
String2 DB 5 dup(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
CLD
MOV CX, 5
LEA SI, String1
LEA DI, String2
REP MOVSB
DELAY USING COUNTER
11
NESTED DELAY
12
DELAY IN 8086
MICROPROCESSORS
CONTINUED
15
Procedure
Used to define subroutines, offers modular programming.
Call to procedure will be a transfer of control to called procedure
during run time.
PROC: indicates beginning of procedure.
Procedure type helps assembler to decide weather to code return as
near/far.
Near/Far term follows PROC indicates type of procedure.[Near by
default]
ENDP: indicates assembler the end of procedure
RET
Name ENDP
Procedure type
NEAR (statement that calls procedure in same segment
with procedure)
FAR (statement that calls procedure in different segment)
Default type is near
Procedure Invocation
CALL Name
PROCEDURES – CONT.
Executing a CALL instruction causes
Save return address on the stack
Near procedure: PUSH IP
Far procedure: PUSH CS; PUSH IP
IPgets the offset address of the first instruction of the
procedure
CS gets new segment number if procedure is far
Executing a RET instruction causes
Transfer control back to calling procedure
Near procedure: POP IP
Far procedure: POP IP; POP CS
RET n
IP [SP+1:SP]
SP SP + 2 + n
UPPERCASE TO LOWER CASE
CONVERSION USING PROCEDURE
.model small
.stack 100h
cr equ 13
lf equ 10
.data
msg1 db 'enter an upper case letter: $'
result db cr,lf,'The lower case equivalent is:', cr,lf,'$'
.code
start:
mov ax , @data
mov ds,ax
mov dx, offset msg1
19
UPPERCASE TO LOWER CASE
CONVERSION USING PROCEDURE
call outputs
call getc
mov bl,al
add bl,32
mov dx,offset result
call outputs
mov dl,bl
call putc
mov ax,4c00h
int 21h
20
UPPERCASE TO LOWER CASE
CONVERSION USING PROCEDURE
putc proc
mov ah,2h
int 21h
ret
getc proc
mov ah,01h
int 21h
ret
outputs proc
mov ah,9h
int 21h
ret
end start
21
MACRO definition directive
Used to define macro constants.
Call to macro will be replaced by its body during assembly
time.
EQU: macro symbol
MACRO: informs assembler the beginning of macro. It is a
open subroutines. It gets expanded when call is made to
it.
MacroName MACRO [arg1,arg2…argn]
Advantage: save great amount of effort and time by
avoiding overhead of writing repeated pattern of code.
ENDM: informs assembler the end of macro.
COMPARISON OF MACRO CALLS
WITH PROCEDURE CALLS.
MACRO VSS. PROCEDURES
24
MACRO EXAMPLE
End!
26