Advertisement
Eternoseeker

ass 3

Jun 5th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 2.57 KB | Source Code | 0 0
  1. section .data
  2.     ano db 10, "Assignment No. : 3", 10
  3.         db "Positive / Negative elements from 64 bit array ", 10
  4.     ano_len equ $-ano
  5.     arr64 dq -21H, 5H, -33H, 61H, 62H
  6.     n equ 5 ; n stores number of elements in array
  7.     pmsg db 10, 10, "The number of positive elements from 64 bit array: "
  8.     pmsg_len equ $-pmsg
  9.     nmsg db 10, 10, "The number of negative elements from 64 bit array: "
  10.     nmsg_len equ $-nmsg
  11.  
  12. section .bss
  13.     p_count resq 1; reserves a quadword
  14.     n_count resq 1
  15.     char_ans resb 16
  16.  
  17.     %macro print 2
  18.     mov rax, 1
  19.     mov rdi, 1
  20.     mov rsi, %1
  21.     mov rdx, %2
  22.     syscall
  23.     %endmacro
  24.  
  25.     %macro exit 0
  26.     mov rax, 60
  27.     mov rdi, 0
  28.     syscall
  29.     %endmacro
  30.  
  31. section .text
  32.     global _start
  33.  
  34.     _start:
  35.         mov rsi, arr64 ; rsi points to the first element of array
  36.         mov rcx, n ; n contains number of elements in array
  37.         mov rbx, 0 ; counter for positive numbers
  38.         mov rdx, 0 ; counter for negarive numbers
  39.  
  40.     next_num:
  41.         mov rax, [rsi] ; take number in rax
  42.         rol rax, 1 ; rotate by 1 bit to check sign in the CF
  43.         jc negative ; jump to lavel negative if carry is generated
  44.  
  45.     positive:
  46.         inc rbx ; CF=0 means positive number -> Increment positive count
  47.         jmp next
  48.  
  49.     negative:
  50.         inc rdx ; CF = 1 means negative number -> Increment negative count
  51.     next:
  52.         add rsi, 8 ; make rsi point to next number by adding 8 bytes
  53.         dec rcx ; decrement counter of array elements
  54.         jnz next_num
  55.         mov [p_count], rbx ; Store Positive count
  56.         mov [n_count], rdx ; Store Negative count
  57.         print pmsg, pmsg_len
  58.         mov rax, [p_count] ; Load value in p_count to accumulator
  59.         call disp64_proc ; Call display procedure to display
  60.         print nmsg, nmsg_len
  61.         mov rax, [n_count] ; Load value in n_count to accumulator
  62.         call disp64_proc ; Call display procedure to display
  63.         exit
  64.  
  65.     disp64_proc:
  66.         mov rbx, 10 ; Divisor 10 for decimal
  67.         mov rcx, 2 ; No of digits
  68.         mov rsi, char_ans + 1
  69.  
  70.         cnt:
  71.         mov rdx, 0
  72.         div rbx ; rbx = rbx/rax
  73.         cmp dl, 09h ; Check if number lies between 0 to 9
  74.         jbe add30
  75.         add dl, 07
  76.  
  77.         add30:
  78.             add dl, 30h ; Calculate ascii code
  79.             mov [rsi], dl ; Store it in buffer
  80.             dec rsi ; Point to one byte back
  81.             dec rcx ; Decrement count
  82.             jnz cnt ; Jump to cnt until count is not 0
  83.             print char_ans, 2 ; Display result on screen
  84.             ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement