Computer Organization and Assembly Lan-
guage
                     Lab Manual (Lab 3)
Topic: DOS/ IO                                           service Routine, INT
Instruction, How to read Character input with Echo, How to display a Character,
How to read character input without Echo, How to Display String, Data movement
and arithmetic instruction
                            Course Instructor:
                              Lab Instructor:
                                Session: Fall 2021
                  School of Systems and Technology
                         UMT Lahore Pakistan
Objectives: INTRODUCTION to INPUT / OUTPUT INSRTUCTION
Multiplication of 2 Numbers
.model small
.stack 100h
.data
  num1 db 5          ; Define num1 and assign it the value 5
  num2 db 3          ; Define num2 and assign it the value 3
  result dw ?       ; Define result as a word (16-bit) with an undefined initial value
.code
main proc
  mov ax, @data
  mov ds, ax        ; Initialize data segment
  mov al, num1        ; Load num1 into AL
  mov bl, num2        ; Load num2 into BL
  mul bl           ; Multiply AL by BL, result in AX
  mov result, ax      ; Store the result in the variable result
  mov ax, 4C00h        ; Terminate the program
  int 21h          ; DOS interrupt to exit
main endp
end main
Divide 2 numbers.
.model small
.stack 100h
.data
  dividend db 10          ; Define dividend and assign it the value 10
  divisor db 2          ; Define divisor and assign it the value 2
  quotient db ?          ; Define quotient with an undefined initial value
  remainder db ?          ; Define remainder with an undefined initial value
.code
main proc
  mov ax, @data
  mov ds, ax            ; Initialize data segment
  mov al, dividend        ; Load dividend into AL
  mov bl, divisor        ; Load divisor into BL
  xor ah, ah           ; Clear AH for division
  div bl              ; Divide AX by BL, quotient in AL, remainder in AH
  mov quotient, al        ; Store the quotient in the variable quotient
  mov remainder, ah         ; Store the remainder in the variable remainder
  mov ax, 4C00h           ; Terminate the program
  int 21h             ; DOS interrupt to exit
main endp
end main
The 8086 assembly language has specific instructions for multiplication (MUL and IMUL) and divi-
sion (DIV and IDIV). These instructions are designed to operate on specific registers and have
unique operand requirements. Here's why these instructions use only one operand compared to
ADD and SUB:
MUL and DIV Instructions
  1. MUL (Unsigned Multiplication) and IMUL (Signed Multiplication)
      These instructions take a single operand and implicitly use specific registers for the oper-
       ation.
      For 8-bit multiplication (MUL or IMUL with an 8-bit operand), the operand is multiplied
       by the value in AL. The result is stored in AX.
      For 16-bit multiplication (MUL or IMUL with a 16-bit operand), the operand is multi-
       plied by the value in AX. The result is stored in DX:AX (a 32-bit result).
  2. DIV (Unsigned Division) and IDIV (Signed Division)
      These instructions also take a single operand and implicitly use specific registers for the
       operation.
      For 8-bit division (DIV or IDIV with an 8-bit operand), the dividend is in AX. The quotient
       is stored in AL, and the remainder is stored in AH.
      For 16-bit division (DIV or IDIV with a 16-bit operand), the dividend is in DX:AX. The
       quotient is stored in AX, and the remainder is stored in DX.
                        8086 Input /Output Instructions
DOS /IO service Routines
CPU communication with peripherals devices through I/O registers called I/O ports.
They are two instructions, IN and OUT, that access the ports directly. These Instructions are
used when a program needs to directly communicate with peripherals like when fast I/O is
needed.
These are two categories of I/O service routines
(i) BIOS Routines
BIOS routines are stored in ROM and communicate directly with I/O ports.
(ii) DOS Routines
The DOS routines actually use The BIOS routines to perform direct I/O operations. They Can
carry out more complex tasks; for example, printing a character string, getting inputs from key
board, etc.
INT instruction
To invoke DOS or BIOS routine, the INT (interrupt) instruction is used, it has the format
                                          INT interrupt number
Interrupt number is number that specifies a routine, for example, INT 16h invokes a BIOS
routine that performs keyboard input. INT 21h may be used to invoke a large number of DOS
function.
Interrupts Recommended:
1) INT 21H, Function 04CH terminate the code properly and return to the DOS Prompt.
2) INT 21H, Function 02H Display a number or character on the screen.
3) INT 21H, Function 09H Display the string on the screen
4) INT 21H, Function 01H Get Character input with Echo.
5) INT 21h, function 08H Get Character input without Echo.
Function 01 - Read a character input with Echo from keyboard
Reads a character from the standard input device and echoes it to the standard output device.
If no character is ready it waits until one is available.
To get input from the keyboard executes the following instruction.
                 MOV ah, 01H; input key function
                 INT 21H; get ASCII code in AL
On entry:            Ah= 01H
Returns:             AL= 8 bit data input.
When a character is pressed, AL will contain its ASCII code if any other key is pressed, such as
an arrow key or F0-F10 and so on, AL will contain 0.
Function 02 - Display a Character on Screen
To display a character “A‟ on screen execute the following instruction, actually DL must contain
the ASCII code of Character that is to be displayed. In this example DL will contain ASCI code
of “A‟.
                                     MOV AH, 2; display character function
                                     MOV DL, “A”; character is A
                                    INT 21H ; display character
                  On entry:           AH = 02h
                                     DL = 8 bit data (usually ASCII character)
                   Returns:          Nothing
Function 08 - Read a character input without Echo from keyboard
Reads a character from the standard input device without copying it to the display. If no
character is ready it waits until one is available.
                                    MOV AH, 08h ; Input key function
                                    INT 21h    ; get ASCII code in AL.
                                   On entry:     Ah= 01H
                                   Returns:     AL= 8 bit data input.
Function 09 - Display a string on Screen
To display a String execute the following instruction, DX register must contain the starting offset
Address of string that to be displayed
   mov ax, @data
   mov ds, ax   ; Initialize data segment
   ; Print Msg
   lea dx, Msg ; Load address of name into DX
   mov ah, 09h ; DOS interrupt function 09h (print string)
   int 21h     ; Call DOS interrupt
On entry           Ah= 09H
                  DS: DX = segment: offset of string
Returns:              Nothing.
Where msg is string define in Data Segment as
                 Msg db 'Hello world', 13, 10, '$'
                 or
                 Msg db “hello world” ,13h,10h,24h
The string must be terminated by the $ character (24h), which is not transmitted. Any ASCII
codes can be embedded within the string.
Data movement and Arithmetic instruction
In 8086 General purpose registers are:
                  ax, bx, cx, dx
These four 16-bit registers can also be treated as eight 8-bit registers:
ah, al, bh, bl, ch, cl, dh, dl
Assignment
In C, assignment takes the form:
C   Syntax           Assembly syntax
x = 42 ;              mov x,42
y = 24;               mov y, 24
z = x + y;            add z,x
                     add z ,y
In assembly language we carry out the same operation but we use an instruction to denote the
assignment operator ("=" in C).
                       OPCODE destination, source
                       Operation Operand, Operand
                       mov      Register, register
                       mov      register, immediate value
                       add      register, register
                       add      register, immediate value
Sample CODE
09h Interrupt: It is used for string display. Also known as PUT
String i.e. PUTS ( )
 data_seg segment 'data'
 st1 db "*       *",24h
 st2 db 0dh,0ah," *      *",24h
 st3 db 0dh,0ah, " *     *",24h
 st4 db 0dh,0ah, " * *",24h
 st5 db 0dh,0ah, " * *",24h
 st6 db 0dh,0ah,"    *" ,24h
 data_seg ends
 code_seg segment 'code'
 assume cs:code_seg,ds:data_seg,ss:stack_seg
 main proc
 mov ax,data_seg
 mov ds,ax
 mov dx,offset st1
 mov ah,09h
int 21h
mov dx,offset st2
mov ah,09h
int 21h
mov dx,offset st3
mov ah,09h
int 21h
mov dx,offset st4
mov ah,09h
int 21h
mov dx,offset st5
mov ah,09h
int 21h
mov dx,offset st6 ;
mov ah,09h
int 21h
mov ah,4ch ;4ch interrupt for termination/exit
int 21h
endp
code_seg ends
end main
Sample Code:
01h Interrupt: It is used for character input (8 bit) from user. Also
known as GET CHARACTER WITH ECHO i.e. GETCHE ( ).
.model small
.stack 100h
.data
  st1 db "Enter a character: ",13,10,'$'
.code
main proc
   mov ax,@data ;moves memory location of data segment into ax (16 bit) register
   mov ds,ax        ;moves data address to data segment register so that data segment gets
           ;initialized and load memory variables faster
  mov dx,offset st1 ;offset loads the beginning address of variables into ds:dx
  mov ah,09h       ;ah for service and here 09h is used for string display i.e. Put String
  int 21h      ;DOS interrupt
  mov ah,01h ; It will take a character input from user.
  int 21h ; DOS interrupt
  mov bl,al ; By default char input using 01h interrupt goes to al register. The char
       ; which user enter its hex value will move to al register and then to bl reg
  ; Terminate program
  mov ax, 4C00h
  int 21h
main endp
end main
Sample Code:
02h Interrupt: It is used to display (8 bit) character. Also known as
PUT CHARACTER i.e. PUTCH ( ).
.model small
.stack 100h
.data
  st1 db "Enter a character: ",13,10,'$'
  st2 db 13,10,"You have entered character: ",24h
.code
main proc
   mov ax,@data ;moves memory location of data segment into ax (16 bit) register
   mov ds,ax        ;moves data address to data segment register so that data segment gets
           ;initialized and load memory variables faster
     mov dx,offset st1 ;offset loads the beginning address of variables into ds:dx
     mov ah,09h       ;ah for service and here 09h is used for string display i.e. Put String
     int 21h      ;DOS interrupt
     mov ah,01h ; It will take a character input from user.
     int 21h ; DOS interrupt
     mov bl,al ; By default char input using 01h interrupt goes to al register. The char
          ; which user enter its hex value will move to al register and then to bl reg
     mov dx,offset st2
     mov ah,09h
     int 21h
     mov dl,bl ;move value to dl register for displaying character
     mov ah,02h ;
     int 21h
  ; Terminate program
  mov ax, 4C00h
  int 21h
main endp
end main
                                          LAB TASKS
Lab Task # 1
Write an ALP to print following using PUTS ()
*          *
 *       *
     *
 *      *
*        *
    *
   ***
  ****
 *****
******
Solution :
.model small
.stack 100h
.data
  line1 db 0Dh, 0Ah, "*     *", '$'
  line2 db 0Dh, 0Ah, " *  *", '$'
  line3 db 0Dh, 0Ah, " * *", '$'
  line4 db 0Dh, 0Ah, " *", '$'
  line5 db 0Dh, 0Ah, " * *", '$'
  line6 db 0Dh, 0Ah, "*  *", '$'
.code
main proc
 ; Initialize data segment
 mov ax, @data
 mov ds, ax
  ; Print each line of the pattern
  mov dx, offset line1
  mov ah, 09h
  int 21h
  mov dx, offset line2
  mov ah, 09h
  int 21h
  mov dx, offset line3
  mov ah, 09h
  int 21h
  mov dx, offset line4
  mov ah, 09h
  int 21h
  mov dx, offset line5
  mov ah, 09h
  int 21h
  mov dx, offset line6
  mov ah, 09h
  int 21h
  ; Terminate the program
  mov ah, 4Ch
  int 21h
main endp
end main
Lab Task # 2
1. Prompt user to enter a lower case letter character
2. Convert it into Upper Case character
3. Output the result in the following format:
                  Enter a lower case character: f
                  Lower case to upper case: F
Solution
.model small
.stack 100h
.data
  prompt db 'Enter a lower case character: $'
  result_msg db 0Dh, 0Ah, 'Lower case to upper case: $'
  newline db 0Dh, 0Ah, '$'
  input_char db ? ; Variable to store the input character
.code
main proc
  ; Initialize data segment
  mov ax, @data
  mov ds, ax
  ; Display prompt message
  lea dx, prompt
  mov ah, 09h
  int 21h
  ; Read a character from keyboard
  mov ah, 01h
  int 21h
  mov input_char, al ; Store the input character in `input_char`
  ; Convert to uppercase if the character is a lowercase letter
  mov al, input_char
  sub al, 20h   ; Convert to uppercase by subtracting 32 (0x20)
  mov input_char, al ; Store the uppercase character back in `input_char`
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Display result message
  lea dx, result_msg
  mov ah, 09h
  int 21h
  ; Display the converted character
  mov dl, input_char
  mov ah, 02h       ; DOS function to display a single character
  int 21h
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Terminate the program
  mov ah, 4Ch
  int 21h
main endp
end main
Task 3:
Write an assembly program to print your name using @ sign.
Solution :
.model small
.stack 100h
.data
  ; Define each line of the pattern for the name using '@' characters
  line1 db 0Dh, 0Ah, '@@@@@ @@@@@ @@@@ @@@@', '$'
  line2 db 0Dh, 0Ah, '@ @ @ @ @ @ @ @', '$'
  line3 db 0Dh, 0Ah, '@@@@@ @@@@@ @@@@@ @@@@@', '$'
  line4 db 0Dh, 0Ah, '@ @ @ @ @ @ @ @', '$'
  line5 db 0Dh, 0Ah, '@ @ @ @ @ @ @ @', '$'
.code
main proc
  ; Initialize data segment
  mov ax, @data
  mov ds, ax
  ; Print each line of the pattern
  mov dx, offset line1
  mov ah, 09h
  int 21h
  mov dx, offset line2
  mov ah, 09h
  int 21h
  mov dx, offset line3
  mov ah, 09h
  int 21h
  mov dx, offset line4
  mov ah, 09h
  int 21h
  mov dx, offset line5
  mov ah, 09h
  int 21h
  ; Terminate the program
  mov ah, 4Ch
  int 21h
main endp
end main
Task 4:
Write an assembly language program to read a character from the keyboard, store it in memory,
and display it back to the screen.
Solution :
.model small
.stack 100h
.data
  prompt db 'Enter a character: $'
  newline db 0Dh, 0Ah, '$'
  char db ? ; Variable to store the input character
.code
main proc
  ; Initialize data segment
  mov ax, @data
  mov ds, ax
  ; Display prompt message
  lea dx, prompt
  mov ah, 09h
  int 21h
  ; Read a character from keyboard
  mov ah, 01h
  int 21h
  mov char, al ; Store the input character in 'char'
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Display the stored character
  mov dl, char
  mov ah, 02h ; DOS function to display a single character
  int 21h
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Terminate the program
  mov ax, 4C00h
  int 21h
main endp
end main
Task 5:
Write an assembly language program to prompt the user to enter an uppercase letter and
convert it to a lowercase letter.
Task 6:
Write an assembly language program to prompt the user to enter their name and then display
the message "Hello, <name>!".
Solution :
.model small
.stack 100h
.data
  prompt db 'Enter your name: $'
  nam db 20 dup('$') ; Reserve 20 bytes for the name
  msg db 0Dh, 0Ah, 'Hello, $'
  newline db 0Dh, 0Ah, '$'
.code
main proc
  ; Initialize data segment
  mov ax, @data
  mov ds, ax
  ; Display prompt message
  lea dx, prompt
  mov ah, 09h
  int 21h
  ; Read the name from keyboard
  lea dx, nam
  mov ah, 0Ah
  int 21h
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Display greeting message
  lea dx, msg
  mov ah, 09h
  int 21h
  ; Display the name
  lea dx, nam+1 ; Skip the first byte which contains the length
  mov ah, 09h
  int 21h
  ; Display newline
  lea dx, newline
  mov ah, 09h
  int 21h
  ; Terminate the program
  mov ax, 4C00h
  int 21h
main endp
end main
Task 7
Write an assembly language program to display the string in reverse order.
Solution:
.model small
.stack 100h
.data
  msg db 'Hello' , '$'
  stlen equ $-msg
.code
main proc
  ; Initialize data segment
  mov ax, @data
  mov ds, ax
 mov si,0
 mov cx,stlen
 dec cx
 putstack:
  push [si]
  inc si
  loop putstack
 mov cx,stlen
 dec cx
 disp:
   pop dx
   mov ah,02h
   int 21h
   loop disp
  ; Terminate the program
  mov ax, 4C00h
  int 21h
main endp
end main
Task 8
Write an assembly language program to prompt the user to enter a string and then display the
string in reverse order.