0% found this document useful (0 votes)
6 views4 pages

Practical No 9 Qasim 102

The document outlines practical tasks involving loops and character output in assembly language. It includes code snippets for three tasks: printing a pattern of asterisks, counting uppercase letters in a user-input sentence, and printing a numerical pattern. Each task is accompanied by a description of the program's functionality and expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Practical No 9 Qasim 102

The document outlines practical tasks involving loops and character output in assembly language. It includes code snippets for three tasks: printing a pattern of asterisks, counting uppercase letters in a user-input sentence, and printing a numerical pattern. Each task is accompanied by a description of the program's functionality and expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

9: Implementation of Loops, Nested Loops,


Labels, Increment Statements – Tasks
Name: M. Qasim

Roll: 102

Task 1: Run the provided program and check the output.

Program:

Code snippet
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax

mov bx, 1 ; Initialize BX to 1 [cite: 68]


mov cx, 5 ; Initialize CX (outer loop counter) to 5 [cite: 69]

L1: ; Outer loop label [cite: 69]


push cx ; Save outer loop counter on stack [cite: 69]
mov cx, bx ; Set inner loop counter (CX) to current value of BX
[cite: 70]

L2: ; Inner loop label [cite: 71]


Mov dl, '*' ; Load '*' into DL for display [cite: 72]
mov ah,2 ; Set AH to 2 (DOS print character function) [cite:
73]
int 21h ; Call DOS to print character in DL [cite: 74, 75]
loop L2 ; Decrement CX and jump to L2 if CX is not zero
[cite: 76]

mov dl,10 ; Load newline character into DL [cite: 77]


mov ah, 2 ; Set AH to 2 (DOS print character function) [cite:
78]
int 21h ; Call DOS to print newline [cite: 78]

mov dl,13 ; Load carriage return character into DL [cite: 79]


mov ah, 2 ; Set AH to 2 (DOS print character function) [cite:
80]
int 21h ; Call DOS to print carriage return [cite: 81]

inc bl ; Increment BL (used to control inner loop


iterations indirectly) [cite: 82]
pop cx ; Restore outer loop counter from stack [cite: 83]
loop L1 ; Decrement CX and jump to L1 if CX is not zero
[cite: 84]
mov ah,4ch
int 21h
main endp
end main

Output:
*
**
***
****
*****

Task 2: Program to print total number of uppercase letters in a sentence


entered using a while loop.

Code snippet
.model small
.stack 100h
.data
prompt DB 'Enter a sentence (max 80 chars, end with $):', 10,
13, '$'
input_buffer DB 81 DUP(?)
msg_count DB 'Number of uppercase letters: ', '$'
count DB 0

.code
main proc
mov ax, @data
mov ds, ax

; Display prompt for user input


mov ah, 9
lea dx, prompt
int 21h

; Read string from user


mov ah, 0Ah
lea dx, input_buffer
int 21h

; Initialize counter for uppercase letters


mov bl, 0 ; BL will store the count
mov si, offset input_buffer + 2 ; SI points to the actual string
data (after max length and actual length bytes)

; While loop to iterate through the string


while_loop:
mov al, [si] ; Get current character
cmp al, '$' ; Check for string terminator
je end_while ; If terminator, exit loop

cmp al, 'A' ; Check if character is greater than or


equal to 'A'
jl next_char ; If less, not an uppercase letter, skip
cmp al, 'Z' ; Check if character is less than or equal
to 'Z'
jg next_char ; If greater, not an uppercase letter, skip

inc bl ; If it's an uppercase letter, increment


count

next_char:
inc si ; Move to next character
jmp while_loop ; Continue loop

end_while:
; Display the count message
mov ah, 9
lea dx, msg_count
int 21h

; Convert count (in BL) to ASCII and display


mov al, bl
add al, '0' ; Convert numeric value to ASCII character
mov dl, al
mov ah, 2
int 21h

; Exit program
mov ah, 4ch
int 21h
main endp
end main

Task 3: Program to print the following pattern:


1
22
333
4444
55555
.model small
.stack 100h
.data
.code
main proc
mov ax, @data
mov ds, ax

mov bl, 1 ; BL will hold the number to print and the outer loop
counter (1 to 5)

outer_loop:
cmp bl, 6 ; Check if BL is greater than 5 (loop for
numbers 1 to 5)
je exit_program
mov cl, bl ; CL will be the inner loop counter, initialized
with the value of BL

inner_loop:
mov al, bl ; Get the number to print
add al, '0' ; Convert number to its ASCII character
mov dl, al ; Move ASCII character to DL for display
mov ah, 2 ; DOS function to display character
int 21h ; Call DOS

loop inner_loop ; Decrement CL and loop if not zero

; Print a new line after each row


mov dl, 10 ; Newline character
mov ah, 2
int 21h
mov dl, 13 ; Carriage return character
mov ah, 2
int 21h

inc bl ; Increment BL for the next number/row


jmp outer_loop

exit_program:
mov ah, 4ch ; DOS function to exit program
int 21h
main endp
end main

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy