count positive and negative elements
Assembly
section .data
array dq -1h, 74h, 24h, -45h, 20h
n equ 5
p1_msg db 10,"the no of +ve elements from array"
p1_len equ $-p1_msg
p2_msg db 10,"the no of -ve elements from array"
p2_len equ $-p2_msg
section .bss
p_count resq 1
n_count resq 1
char_ans resb 2
%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,array ;giving register base adress of array
mov rcx,n
;now we will store +ve and -ve count into some register
;so lets take two random register ACCUMILATER will be required for shifting purpose so we will use rbx
mov rbx,0 ;+ve count
mov rdx,0 ;-ve count
;---------------------------now we will work on each element in an array---------
; :so how we will access the element
; first we will go the rsi value ,refer its value at rsi and store that into any register
; so register available with us is available
back:
mov rax,[rsi]
shl rax,1 ;then we will shift rsi by 1 because we are interested in MSB,so we need to shift only once and we will get MSb in our carry flag
;----------------so we will check if carry is generated or not
; jc is if else
jc negative ;where we need to jump ...so we will require label value
; else inceremt the value of rbx
positive:
inc rbx;
jmp next
negative:
inc rdx ;if carry is generated we will inc rdx by 1
next:
; to point the next element we need to increment the rsi
add rsi,8 ;increasing rsi by 8 byte
dec rcx
; till rcx value is not 0 ,we need to repeat iteration from back
jnz back
; so to show we need to store temporarily for that we will move value to rdx and rbx
mov [p_count],rbx
mov [n_count],rdx
print p1_msg,p1_len
print p2_msg,p2_len
call display
exit
display:
mov rbx,16
mov rcx,2
mov rsi,char_ans + 1;
Back: ;becz we want to repeat
mov rdx,0 ;to avoid any garbage value
div rbx ;divison occured ,RDX->stores remainder and RAX->stores quotient
;now remainder is present into dx..which is 1 digit,so we can work on the lower part
;of DX...that is DL...
cmp dl,09h;we will compare with 09h because
;hexadecimal consist of 2 rangeees (0-9) and (A-F)
;we compare because the value could not be passed to the output console as it
;it has to be passed in ASCII format .
;suppose it is not between 0-9
jbe add30 ;jump below equal...if it is below 9 or equal to 9 then jumb below to the lebel anad add 30h
add dl,07h
add30: ;we wre taking one label if it is between 0-9 and add 30h
add dl,30h
;now we will movw to the adress stored by rsi
mov[rsi],dl
;now we will decrement rsi
dec rsi
dec rcx
jnz Back ;jump to back level if not zer0
print char_ans ,2
ret
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.