Quiz 2
Submitted By Ahmad Tariq
Roll Number 56
Section B
Subject Computer Organization & Assembly Language
Submitted To Sir Saleem
Question
1) Given a string, write a program to count the frequency of a specific character
using a loop.
2) Implement bubble sort in Assembly to sort an array in ascending order using
nested loops.
3) Write a program that displays the multiplication table (e.g., for 5) using a loop
Answers
1) Given a string, write a program to count the frequency of a specific character
using a loop.
section .data
input_string db 'Ahmad Tariq', 0 ; null-terminated string
search_char db 'l' ; character to count
count_msg db 'Frequency: ', 0
newline db 10, 0
section .bss
count resb 1 ; space to store count
section .text
global _start
_start:
; initialize pointers and count
mov esi, input_string ; ESI points to the input string
mov al, [search_char] ; AL holds the character to search
mov bl, 0 ; BL will store the count
count_loop:
mov cl, [esi] ; load next character from string
cmp cl, 0 ; check for null terminator
je done ; if zero, end of string
cmp cl, al ; compare with search character
jne skip ; if not equal, skip increment
inc bl ; increase count
skip:
inc esi ; move to next character
jmp count_loop
done:
; store result in memory
mov [count], bl
; convert count to ASCII for printing
add bl, '0'
; print message
mov eax, 4
mov ebx, 1
mov ecx, count_msg
mov edx, 1
int 0x80
; print count
mov eax, 4
mov ebx, 1
mov ecx, count
mov edx, 1
int 0x80
; print newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; exit
mov eax, 1
xor ebx, ebx
int 0x80
2) Implement bubble sort in Assembly to sort an array in ascending order using
nested loops.
movzx ecx, byte [j]
section .data cmp ecx, length - 1 ; if j >=
length - 1, end inner loop
array db 9, 7, 5, 3, 1, 2, 4, 6, 8, 0 ;
array of 10 elements jge increment_i
length equ 10 ; ; Load array[j] into al
number of elements
movzx esi, byte [j]
section .bss
mov al, [array + esi]
i resb 1
; Load array[j+1] into bl
j resb 1
inc esi
temp resb 1
mov bl, [array + esi]
section .text
; Compare al and bl
global _start
cmp al, bl
_start:
jbe no_swap ; if al <= bl, no
outer_loop: swap needed
mov byte [i], 0 ; Swap array[j] and array[j+1]
next_i: mov [temp], al
movzx ecx, byte [i] mov [array + esi - 1], bl
cmp ecx, length - 1 ; if i >= mov [array + esi], [temp]
length - 1, end outer loop
no_swap:
jge done
; increment j
mov byte [j], 0
movzx ecx, byte [j]
inner_loop:
inc ecx
mov [j], cl jmp next_i
jmp inner_loop done:
increment_i: ; Exit program
; increment i mov eax, 1 ; syscall: exit
movzx ecx, byte [i] xor ebx, ebx ; return 0
inc ecx int 0x80
mov [i], cl
3) Write a program that displays the multiplication table (e.g., for 5) using a loop
section .data ; Print " x "
num db 5 mov eax, msg
newline db 10, 0 call print_string
msg db " x ", 0 ; Print i
eq db " = ", 0 movzx eax, byte [i]
section .bss call print_num
i resb 1 ; Print " = "
result resb 2 mov eax, eq
buffer resb 4 call print_string
section .text ; Multiply: 5 * i
global _start movzx eax, byte [num]
_start: movzx ebx, byte [i]
mov byte [i], 1 mul bl
print_loop: ; Print result
movzx eax, byte [i] call print_num
cmp eax, 10 ; while i <= 10 ; Newline
jg exit mov eax, newline
; Print 5 call print_string
mov al, [num] ; Increment i
call print_num inc byte [i]
jmp print_loop jnz .next_digit
exit: mov eax, ecx
mov eax, 1 call print_string
xor ebx, ebx ret
int 0x80 print_string:
print_num: push eax
mov ecx, buffer + 3 ; end of mov ecx, eax
buffer
mov edx, 0
mov byte ecx, 0 ; null-
.count_loop:
terminate
cmp byte [ecx + edx], 0
.next_digit:
je .found_len
xor edx, edx
inc edx
mov ebx, 10
jmp .count_loop
div ebx ; eax = eax / 10,
remainder in edx .found_len:
add dl, '0' ; convert digit to mov eax, 4 ; syscall: write
ASCII mov ebx, 1 ; stdout
dec ecx int 0x80
mov [ecx], dl pop eax
test eax, eax ret