section .data

welmsg db 10,'Piyush savale 41',10
welmsg_len equ $-welmsg

pmsg db 10,'Count of +ve numbers::'
pmsg_len equ $-pmsg

nmsg db 10,'Count of -ve numbers::'
nmsg_len equ $-nmsg

nwline db 10

array dw 8505h,90ffh,87h,88h,8a9fh,0adh,02h,8507h

arrcnt equ 8

pcnt db 0
ncnt db 0

section .bss
	dispbuff resb 2

%macro print 2                  ;defining print function
mov eax, 4              ; this 4 commands signifies the print sequence
mov ebx, 1                               
mov ecx, %1             ; first parameter
mov edx, %2             ;second parameter
int 80h                 ;interrupt command      
%endmacro

section .text 		   ;code segment
global _start       ;must be declared for linker
_start:            ;tells linker the entry point ;i.e start of code
print welmsg,welmsg_len   ;print title
mov esi,array
mov ecx,arrcnt       ;store array count in extended counter reg    


up1:                         ;label
bt word[esi],15
;bit test the array number (15th byte) pointed by esi. 
;It sets the carray flag as the bit tested
jnc pnxt     ;jump if no carry to label pskip

inc byte[ncnt]   ;if the 15th bit is 1 it signifies it is a ;negative no and so we ;use this command to increment ncnt counter.
jmp pskip       ;unconditional jump to label skip

pnxt: inc byte[pcnt]    ;label pnxt if there no carry then it is ;positive no 
;and so pcnt is incremented
pskip: inc esi         ;increment the source index but this ;instruction only increments it by 8 bit but the no’s in array ;are 16 bit word and hence it needs to be incremented twice.

inc esi
loop up1       ;loop it ends as soon as the array end “count” or 

;ecx=0 loop automatically assums ecx has the counter

print pmsg,pmsg_len      ;prints pmsg
mov bl,[pcnt]    ;move the positive no count  to lower 8 bit of B reg 
call disp8num            ;call disp8num subroutine
print nmsg,nmsg_len         ;prints nmsg 
mov bl,[ncnt]    ;move the negative no count to lower 8 bits of b reg
call disp8num        ;call disp8num subroutine


print nwline,1        ;New line char

exit:
mov eax,01
mov ebx,0
int 80h

disp8num:
mov ecx,2        ;move 2 in ecx ;Number digits to display
mov edi,dispbuff               ;Temp buffer

	dup1:      ;this command sequence which converts hex to bcd
	rol bl,4  	;Rotate number from bl to get MS digit to LS digit
mov al,bl         ;Move  bl i.e. rotated number to AL
and al,0fh          ;Mask upper digit (logical AND the contents ;of lower8 bits of accumulator with 0fh )

cmp al,09          ;Compare al with 9

      jbe dskip  	;If number below or equal to 9 go to add only 30h
     ;add al,07h ;Else first add 07h to accumulator
	
dskip:
      add al,30h        ;Add 30h to accumulator 
	 mov [edi],al           ;Store ASCII code in temp buff (move contents       ;of accumulator to the location pointed by edi)
 inc edi 
                       ;Increment destination index i.e. pointer to      ;next location in temp buff
loop dup1         ;repeat till ecx becomes zero

print dispbuff,2         ;display the value from temp buff
ret                      ;return to calling program
 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret