Assembly Language 3
Assembly Language 3
CourseCode
DateAssigned
:ComputerOrganisation
:TDB1123/TCB1043
:Week11
________________________________________________________________________
1.0 Displaying a String
In our first program, we used INT 21h, functions 1 and 2, to read and display a single character. Here is
another INT 21h function that can be used to display a character string:
INT 21h, Function 9:
Display a String
Input : DX = offset address of string.
The string must end with a $ character.
The $ marks the end of the string and is not displayed. If the string contains the ASCII code of a control
character, the control function is performed.
To demonstrate this function, we will write a program that prints HELLO! on the screen. This message is
defined in the data segment as
MSG
DB
HELLO!$
destination, source
Where destination is a general register and source is a memory location. LEA stands for Load Effective
Address. It puts a copy of the source-offset address into the destination. For example,
LEA
DX, MSG
AX, @DATA
DS, AX
@DATA is the name of the data segment defined by DATA. The assembler translates the name @DATA
into a segment number. Two instructions are needed because a number (the data segment number) may not
be moved directly into a segment register.
With DS initialized, we may print the HELLO! message by placing its address in DX and executing INT
21h:
LEA
DX, MSG
; get message
MOV AH, 9
; display string function
____________________________________________________________________________________________________________
SaipunidzamMahamad@ComputerOrganisation
INT
21h
; display string
EQU
EQU
0DH
0AH
The messages and the input character can be stored in the data segment like this:
MSG1
MSG2
CHAR
DB
DB
DB
In defining MSG2 and CHAR, we have used a helpful trick: because the program is supposed to
display the second message and the letter (after conversion to upper case) on the next line, MSG2 starts
with the ASCII codes for carriage return and line feed; when MSG2 is displayed with INT 21h, function 9,
these control functions are executed and the output is displayed on the next line. Because MSG2 does not
end with $,INT 21h goes on and displays the character stored in CHAR.
Our program begins by displaying the first message and reading the character:
LEA
MOV
INT
MOV
INT
DX, MSG1
AH, 9
21H
AH, 1
21H
Having read a lowercase letter, the program must convert it to upper case. In the ASCII character sequence,
the lowercase letters begin at 61h and the uppercase letters start at 41h, so subtraction of 20h from the
contents of AL does the conversion:
SUB
MOV
AL, 20H
CHAR, AL
Now the program displays the second message and the uppercase letter:
LEA
DX, MSG2
; get second message
MOV AH, 9
; display string function
INT
21H
; display message and upper case letter in front
____________________________________________________________________________________________________________
SaipunidzamMahamad@ComputerOrganisation
2.0 Exercises
2.1
AX, @DATA
DS, AX ; initialize DS
; display message
LEA
DX, MSG
MOV AH, 9
INT
21h
; return to DOS
MOV AH, 4CH
INT
21h
MAIN ENDP
END
MAIN
2.2
; get message
; display string function
; display message
; DOS exit
INT
21H
MAIN ENDP
END MAIN
2.3
; DOS exit
PRINT_ASCII
2.3.2 Program 2
TITLE Codes2 : IBM CHARACTER DISPLAY
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,2
; display character function
MOV CX, 256 ; no. of chars to display
MOV DL, 0
; DL has ASCII code of null char
PRINT_LOOP :
INT 21H
; display a char
INC DL ; increment ASCII code
DEC CX
; decrement counter
JNZ PRINT_LOOP ; keep going if CX not 0
; DOS exit
MOV AH,4CH
____________________________________________________________________________________________________________
SaipunidzamMahamad@ComputerOrganisation
INT 21H
MAIN ENDP
END MAIN
____________________________________________________________________________________________________________
SaipunidzamMahamad@ComputerOrganisation