Microprocessor/Codes/Practical-1.asm
2024-06-10 11:23:57 +05:30

89 lines
2.2 KiB
NASM

; THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
; Problem Statement: Write X86/64 ALP to count number of positive and negative numbers from the array.
; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/
; BEGINNING OF CODE
%macro print 2
mov rax,1 ; Standard output
mov rdi,1 ; Input write
mov rsi,%1 ; Display message address
mov rdx,%2 ; Message length
syscall ; Interrupt for kernel in 64-bit
%endmacro
section .data
m1 db 10,"ALP to count positive and negative numbers from an array",10
l1 equ $-m1
m2 db 10,"The count of positive numbers is:",10
l2 equ $-m2
m3 db 10,"The count of negative numbers is:",10
l3 equ $-m3
;array dw 8132h,6879h,711Ah,3567h,4567h
;array dd 81328132h,12346879h,6735711Ah,34563567h,67894567h
array dq 81328132ABCDh,12346879AAAAh,6735711AFFFFh,34563567EEEEh,67894567DEFAh
newline db 10
pcnt db 00
ncnt db 00
section .bss
displaybuffer resb 2
section .text
global _start:
_start:
print m1,l1
mov rsi,array
mov rcx,05
up:
bt qword [rsi],63 ; Check the most significant bit for negativity
jnc pnxt ; If not negative, jump to pnxt
inc byte[ncnt] ; Increment negative count
jmp pskip ; Jump to pskip
pnxt: inc byte[pcnt] ; Increment positive count
pskip: add rsi,8 ; Move to next 8 bytes
loop up ; Loop until rcx becomes zero
print m2,l2
mov bl,[pcnt]
call display
print newline,1
print m3,l3
mov bl,[ncnt]
call display
print newline,1
mov rax,60
syscall
display:
mov rdi,displaybuffer ; Destination for displaying
mov rcx,02 ; Display 2 characters
dloop:
rol bl,04 ; Rotate left by 4 bits
mov dl,bl ; Move contents of bl to dl
and dl,0fh ; Mask out upper bits
add dl,30h ; Convert to ASCII
cmp dl,39h ; Compare if greater than '9'
jbe skip ; Jump if below or equal
add dl,07h ; Adjust for letters
skip:
mov [rdi],dl ; Store the character
inc rdi ; Move to next position
loop dloop ; Loop until rcx becomes zero
print displaybuffer,2 ; Print the display buffer
ret
; END OF CODE