LA2-count Number of Positive and Negative Numbers From The Array
LA2-count Number of Positive and Negative Numbers From The Array
EXP NO: 02
AIM: Write an X86/64 ALP to count number of positive and negative numbers from the array.
OBJECTIVES:
ENVIRONMENT:
THEORY:
Mathematical numbers are generally made up of a sign and a value (magnitude) in which the
sign indicates whether the number is positive, ( + ) or negative, ( – ) with the value indicating the
size of the number, for example 23, +156 or -274. Presenting numbers is this fashion is called
“sign-magnitude” representation since the left most digit can be used to indicate the sign and the
remaining digits the magnitude or value of the number.
Sign-magnitude notation is the simplest and one of the most common methods of representing
positive and negative numbers either side of zero, (0). Thus negative numbers are obtained
simply by changing the sign of the corresponding positive number as each positive or unsigned
number will have a signed opposite, for example, +2 and -2, +10 and -10, etc.
But how do we represent signed binary numbers if all we have is a bunch of one’s and zero’s.
We know that binary digits, or bits only have two values, either a “1” or a “0” and conveniently
for us, a sign also has only two values, being a “+” or a “–“.
Then we can use a single bit to identify the sign of a signed binary number as being positive or
negative in value. So to represent a positive binary number (+n) and a negative (-n) binary
number, we can use them with the addition of a sign.
For signed binary numbers the most significant bit (MSB) is used as the sign bit. If the sign bit is
“0”, this means the number is positive in value. If the sign bit is “1”, then the number is negative
in value. The remaining bits in the number are used to represent the magnitude of the binary
number in the usual unsigned binary number format way.
Then we can see that the Sign-and-Magnitude (SM) notation stores positive and negative values
by dividing the “n” total bits into two parts: 1 bit for the sign and n–1 bits for the value which is
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
a pure binary number. For example, the decimal number 53 can be expressed as an 8-bit signed
binary number as follows.
ALGORITHM:
STEP 1: Initialize index register with the offset of array of signed numbers
STEP 2: Initialize ECX with array element count
STEP 3: Initialize positive number count and negative number count to zero
STEP 4: Perform MSB test of array element
STEP 5: If set jump to step 7
STEP 6: Else Increment positive number count and jump to step 8
STEP 7: Increment negative number count and continue
STEP 8: Point index register to the next element
STEP 9: Decrement the array element count from ECX, if not zero jump to step 4, else continue
STEP 10: Display Positive number message and then display positive number count
STEP 11: Display Negative number message and then display negative number count
STEP 12: EXIT
FLOWCHART:
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
PROGRAM:
;Write an ALP to count no. of positive and negative numbers from the array.
section .data
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
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
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
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
inc esi
loop up1 ;loop it ends as soon as the array end “count” or
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
dskip:
add al,30h ;Add 30h to accumulator
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
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
OUTPUT:
CONCLUSION:
Hence we counted and displayed the number of positive and negative numbers from the
array of signed numbers.
Oral Questions:
1. Write Down Write, Read, Exit System Call for 32bits Operating System?
2. What is the difference between Macros & Procedure?
3. Explain BT Instruction?
4. Explain Significance of Sign Bit?
5. Explain How Loop Instruction Works?
6. Explain resb,resw,resq, db, dw,dq?
7. Explain the role of Section .data, section .bss, section .text?