Professional Documents
Culture Documents
@@chapter 4.2 Introduction To Assembly Language
@@chapter 4.2 Introduction To Assembly Language
mov dx,offset \n
mov ah,9h
int 21h
mov dx,offset array+2
mov ah,9h
int 21h
end start
6
.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
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 ?, ‘$’
.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
.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
11
12
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
Executing a CALL instruction causes
Save return address on the stack
Near procedure: PUSH IP
Far procedure: PUSH CS; PUSH IP
IP gets 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
.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
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
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.
24
26