Docsity

assembly exercises chapter 4, Exercises of Computer Science

Toggle sidebar
bg1
EXAMPLE 1 : about inputs and outputs (problem 9 from the book) An
Assembly Language Program that :
a. prompt the user to enter 3 initials
b. read first, middle, and last initials of a person's name
c. display each character in a new line
.MODEL SMALL
.STACK 100H
.DATA
LF EQU 0AH
CR EQU 0DH
MSG DB CR,LF, ’Enter 3 Characters : $'
NewLine DB LF,CR,'$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
; the next 3 line of code will allow us to print something on
the ; screen , in this case it will print out MSG
MOV AH,9 ; display string function
LEA DX,MSG ; get message
INT 21H ; display message
; the next 3 line of code will allow us to take a single input from
;the user which is the first initial and save it BL
MOV AH,1 ; read character function
INT 21H ; characte in AL
MOV BL,AL ; save character in BL
; the next 3 line of code will allow us to take a single input from
;the user which is the second initial and save it in BH
MOV AH,1 ; read character function
LAB CS220
pf3
pf4
pf5

Partial preview of the text

Download assembly exercises chapter 4 and more Exercises Computer Science in PDF only on Docsity!

EXAMPLE 1 : about inputs and outputs (problem 9 from the book) An

Assembly Language Program that :

a. prompt the user to enter 3 initials

b. read first, middle, and last^ initials^ of^ a person's^ name c. display each character in a new line

.MODEL SMALL

.STACK 100H

.DATA

LF EQU 0AH

CR EQU 0DH

MSG DB CR,LF, ’Enter 3 Characters : $' NewLine DB LF,CR,'$' .CODE

MAIN PROC

MOV AX,@DATA

MOV DS,AX

; the next 3 line of code will allow us to print something on the ; screen , in this case it will print out MSG

MOV AH,9 ; display string function LEA DX,MSG ; get message INT 21H ; display message

; the next 3 line of code will allow us to take a single input from ;the user which is the first initial and save it BL

MOV AH,1 ; read character function INT 21H ; characte in AL MOV BL,AL ; save character in BL

; the next 3 line of code will allow us to take a single input from ;the user which is the second initial and save it in BH

MOV AH,1 ;^ read^ character^ function

INT 21H ; characte in AL MOV BH,AL ; save character in BH

; the next 3 line of code will allow us to take a single input from ; the user which is the third initial and save it in CL

MOV AH,1 ; read character function INT 21H ; characte in AL MOV CL,AL ; save character in CL

; the next 3 line of code will allow us to print something on the ; screen, in this case it will print out an empty new line

MOV AH,9 ; display string function LEA DX,NewLine ; get message INT 21H ;display message

; the next 3 line of code will allow us to print a single character on ;the screen , in this case it will print out the first initial entered ;by the user (saved earlier in the BL)

MOV AH,2 ; display single character function MOV DL,BL ; get character from BL INT 21H ; display character

; the next 3 line of code will allow us to print something on the ; screen, in this case it will print out an empty new line again

MOV AH,9 ; display string function LEA DX,NewLine ; get message INT 21H ;display message

; the next 3 line of code will allow us to print a single character on ;the screen , in this case it will print out the second initial ;entered by the user (saved earlier in the BH)

MOV AH,2 ;^ display^ single^ character^ function MOV DL,BH ; get character from BH INT 21H ; display character

;Solving the same problem in another way instead of assigning each ;character to a register we will assign each character to a variable

. MODEL SMALL

.STACK 100H .DATA

MSG DB 'ENTER THREE INITIALS: $'

CHAR DB?

CHAR1 DB?

CHAR2 DB?

.CODE

MAIN PROC

MOV AX,@DATA

MOV DS,AX

;DISPLAY MSG

MOV AH,

LEA DX,MSG

INT 21h

;READ FIRST CHARACTER AND STORE IT IN VARIABLE CHAR

MOV AH,

INT 21h

MOV CHAR,AL

;READ SECOND CHARACTER AND STORE IT IN VARIABLE CHAR

MOV AH,

INT 21h

MOV CHAR1,AL ;READ THIRD CHARACTER AND STORE

IT IN VARIABLE CHAR

MOV AH,

INT 21h

MOV CHAR2,AL

;NEW LINE

MOV AH,

MOV DL,0DH

INT 21h

MOV DL,0AH

INT 21h

;PRINT FIRST CHARACTER

MOV AH,

MOV DL,CHAR

INT 21h

;NEW LINE

MOV AH,

MOV DL,0DH

INT 21h

MOV DL,0AH

INT 21h

;PRINT SECOND CHARACTER

MOV AH,

MOV DL,CHAR

INT 21h

;NEW LINE

MOV AH,

MOV DL,0DH

INT 21h

MOV DL,0AH

INT 21h

;PRINT THIRD CHARACTER

MOV AH,

MOV DL,CHAR

INT 21h

MOV AH,4CH

INT 21h

MAIN ENDP

END MAIN

MAIN ENDP

END MAIN