Assignment No- 3
Name:-Gaurav Shingare
Rollno:-2311122
Div:-B
Batch:-C
#Program
%macro scall 4
mov rax, %1
mov rdi, %2
mov rsi, %3
mov rdx, %4
syscall
%endmacro
section .data
arr dq 10, -5, -2, -30 ; Updated array with sample values
n equ 4
pmsg db 10, 13, "The Count of Positive No: ", 10, 13
plen equ $ - pmsg
nmsg db 10, 13, "The Count of Negative No: ", 10, 13
nlen equ $ - nmsg
nwline db 10, 13
section .bss
pcnt resq 1 ; Memory space to store positive count
ncnt resq 1 ; Memory space to store negative count
char_answer resb 20 ; Space for printing the number
section .text
global _start
_start:
mov rsi, arr
mov rdi, n
mov rbx, 0 ; Counter for positive numbers
mov rcx, 0 ; Counter for negative numbers
up:
mov rax, [rsi]
cmp rax, 0
js negative
positive:
inc rbx
jmp next
negative:
inc rcx
next:
add rsi, 8
dec rdi
jnz up
; Store results in memory
mov [pcnt], rbx
mov [ncnt], rcx
; Print "The Count of Positive No: "
scall 1, 1, pmsg, plen
mov rax, [pcnt] ; Load positive count
call print_number
; Print "The Count of Negative No: "
scall 1, 1, nmsg, nlen
mov rax, [ncnt] ; Load negative count
call print_number
scall 1, 1, nwline, 1 ; Print newline
mov rax, 60 ; Exit system call
mov rdi, 0 ; Exit status
syscall
; ==================================
; Number Printing Function
; ==================================
print_number:
mov rsi, char_answer + 19 ; Start from the end of the buffer
mov byte [rsi], 0 ; Null-terminate the string
dec rsi
mov rbx, 10 ; Base 10 divisor for division
mov rcx, 0 ; Flag for negative number handling
cmp rax, 0
jge convert_number
mov rcx, 1 ; Mark it as a negative number
neg rax ; Make the value positive
convert_number:
mov rdx, 0 ; Clear the remainder register
div rbx ; Divide RAX by 10
add dl, '0' ; Convert remainder to ASCII (character)
mov [rsi], dl
dec rsi
test rax, rax ; Check if RAX is 0
jnz convert_number ; If not, continue dividing
cmp rcx, 0
je print_now
mov byte [rsi], '-' ; Add negative sign
dec rsi
print_now:
inc rsi
mov rdx, char_answer + 19
sub rdx, rsi
scall 1, 1, rsi, rdx ; Print the number string
ret
#Output
(base) comp@anl13:~$ nasm -f elf64 xyz.asm
(base) comp@anl13:~$ ld xyz.o
(base) comp@anl13:~$ ./a.out
The Count of Positive No:
1
The Count of Negative No:
3