0% found this document useful (0 votes)
3 views

Assignment_5_MIC

The document is an assembly language program that counts the number of positive and negative integers input by the user until they enter '0'. It initializes counters for positive and negative numbers, reads input, and increments the respective counters based on the input value. Finally, it prints the counts of positive and negative numbers before exiting the program.
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)
3 views

Assignment_5_MIC

The document is an assembly language program that counts the number of positive and negative integers input by the user until they enter '0'. It initializes counters for positive and negative numbers, reads input, and increments the respective counters based on the input value. Finally, it prints the counts of positive and negative numbers before exiting the program.
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/ 3

Assignment No.

5 : Write an ALP to count numbers of positive and


negative numbers from the array.

section .data
prompt db "Enter the numbers (end with 0): ", 0
prompt_len equ $ - prompt
msg_pos db "Count of Positive numbers: ", 0
msg_pos_len equ $ - msg_pos
msg_neg db "Count of Negative numbers: ", 0
msg_neg_len equ $ - msg_neg
newline db 10, 0
buffer db 10 ; Buffer to hold each number input as a string

section .bss
pcount resb 1 ; Count of positive numbers
ncount resb 1 ; Count of negative numbers
num resb 1 ; Temp storage for each number character
input resb 1 ; Variable to hold individual number input

section .text
global _start
_start:
; Initialize counts
mov byte [pcount], 0
mov byte [ncount], 0

; Print prompt
mov rax, 1
mov rdi, 1 ; file descriptor: stdout
mov rsi, prompt ; pointer to prompt
mov rdx, prompt_len; length of prompt
syscall

input_loop:
; Read number input
mov rdi, input ; buffer for input
call read_input
movzx rax, byte [input] ; Load the number (ASCII)

; Check if input is '0' (end marker)


cmp al, '0'
je print_counts ; If user enters 0, exit loop
; Convert ASCII to signed integer
sub rax, '0' ; Convert from ASCII to integer
cmp rax, 0
js count_negative ; Jump if number is negative
inc byte [pcount] ; Increment positive count
jmp input_loop ; Continue input loop

count_negative:
inc byte [ncount] ; Increment negative count
jmp input_loop ; Continue input loop

print_counts:
; Print count of positive numbers
mov rax, 1
mov rdi, 1
mov rsi, msg_pos
mov rdx, msg_pos_len
syscall

; Print positive count


mov al, [pcount] ; Get the positive count
call print_num ; Print positive count

; Print newline
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall

; Print count of negative numbers


mov rax, 1
mov rdi, 1
mov rsi, msg_neg
mov rdx, msg_neg_len
syscall

; Print negative count


mov al, [ncount] ; Get the negative count
call print_num ; Print negative count

; Exit the program


mov rax, 60 ; syscall: exit
xor rdi, rdi ; exit code 0
syscall

; Function to read user input


read_input:
; Reading input from stdin
mov rax, 0 ; syscall: sys_read
mov rdi, 0 ; file descriptor: stdin
mov rsi, input ; buffer for input
mov rdx, 10 ; maximum number of bytes to read
syscall
; Null-terminate the string for safety
mov byte [rsi + rax - 1], 0
ret

; Function to print a single digit number in AL to stdout


print_num:
; If number is 0, print '0'
cmp al, 0
je .print_zero

; Convert to ASCII and print


add al, '0' ; Convert number to ASCII
mov [num], al ; Store digit
mov rax, 1
mov rdi, 1
mov rsi, num
mov rdx, 1
syscall
ret

.print_zero:
; Print '0' directly
mov byte [num], '0'
mov rax, 1
mov rdi, 1
mov rsi, num
mov rdx, 1
syscall
Ret

Output :

You might also like