Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- ano db 10, "Assignment No. : 3", 10
- db "Positive / Negative elements from 64 bit array ", 10
- ano_len equ $-ano
- arr64 dq -21H, 5H, -33H, 61H, 62H
- n equ 5 ; n stores number of elements in array
- pmsg db 10, 10, "The number of positive elements from 64 bit array: "
- pmsg_len equ $-pmsg
- nmsg db 10, 10, "The number of negative elements from 64 bit array: "
- nmsg_len equ $-nmsg
- section .bss
- p_count resq 1; reserves a quadword
- n_count resq 1
- char_ans resb 16
- %macro print 2
- mov rax, 1
- mov rdi, 1
- mov rsi, %1
- mov rdx, %2
- syscall
- %endmacro
- %macro exit 0
- mov rax, 60
- mov rdi, 0
- syscall
- %endmacro
- section .text
- global _start
- _start:
- mov rsi, arr64 ; rsi points to the first element of array
- mov rcx, n ; n contains number of elements in array
- mov rbx, 0 ; counter for positive numbers
- mov rdx, 0 ; counter for negarive numbers
- next_num:
- mov rax, [rsi] ; take number in rax
- rol rax, 1 ; rotate by 1 bit to check sign in the CF
- jc negative ; jump to lavel negative if carry is generated
- positive:
- inc rbx ; CF=0 means positive number -> Increment positive count
- jmp next
- negative:
- inc rdx ; CF = 1 means negative number -> Increment negative count
- next:
- add rsi, 8 ; make rsi point to next number by adding 8 bytes
- dec rcx ; decrement counter of array elements
- jnz next_num
- mov [p_count], rbx ; Store Positive count
- mov [n_count], rdx ; Store Negative count
- print pmsg, pmsg_len
- mov rax, [p_count] ; Load value in p_count to accumulator
- call disp64_proc ; Call display procedure to display
- print nmsg, nmsg_len
- mov rax, [n_count] ; Load value in n_count to accumulator
- call disp64_proc ; Call display procedure to display
- exit
- disp64_proc:
- mov rbx, 10 ; Divisor 10 for decimal
- mov rcx, 2 ; No of digits
- mov rsi, char_ans + 1
- cnt:
- mov rdx, 0
- div rbx ; rbx = rbx/rax
- cmp dl, 09h ; Check if number lies between 0 to 9
- jbe add30
- add dl, 07
- add30:
- add dl, 30h ; Calculate ascii code
- mov [rsi], dl ; Store it in buffer
- dec rsi ; Point to one byte back
- dec rcx ; Decrement count
- jnz cnt ; Jump to cnt until count is not 0
- print char_ans, 2 ; Display result on screen
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement