DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   print odd and even numbers (http://www.daniweb.com/forums/thread19561.html)

pro32 Mar 3rd, 2005 5:13 pm
print odd and even numbers
 
My program takes input from users, sort them and print out the result. But I would also like for it to only print out odd numbers and print out even numbers.Any help is appreciated.

.data

arrayInt DWORD 10 dup(0)
sentence BYTE  "Please input a three digit integer: "

.code
main PROC

        mov esi, 0
        mov ecx, LENGTHOF sentence

L1:
        mov al, sentence[esi]
        call WriteChar
        inc esi
Loop L1

;Filling array
        mov        ecx , LENGTHOF arrayInt
        mov        esi, 0

L2:
        call        ReadInt
        mov        arrayInt[esi], eax
        add        esi, 4
Loop        L2

;Sorting array
L3:
        mov        edx, 0                        ; bubble flag
        mov        ecx, LENGTHOF arrayInt
        dec        ecx
        mov        esi, 0                        ; first elemennt
InnerLoop:
        mov        eax, arrayInt[esi]
        cmp        eax, arrayInt[esi+4]
        jle        OutLoop

; If first element grater then second switch their places
        mov        ebx, arrayInt[esi+4]
        mov        arrayInt[esi], ebx
        mov        arrayInt[esi+4], eax
        mov        edx, 1                        ; set bubble flag
OutLoop:
        add        esi, 4
Loop        InnerLoop
        cmp        edx, 1
        je        L3
       

; Printing array
        mov        ecx, LENGTHOF arrayInt
        mov        esi, OFFSET arrayInt
L4:
        mov        eax, [esi]
        add        esi, 4
       
        call        WriteInt
        call        CrLf
Loop        L4

exit
main ENDP
END main

1o0oBhP Mar 4th, 2005 8:15 am
Re: print odd and even numbers
 
not done assembly for a while but...

in the array loop if you have a flag for odd / even and XOR it every time the loop goes round it will switch from odd to even. Then test the flag to see whether to print or not (an algorithm I use in c++)


All times are GMT -4. The time now is 12:18 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC