News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

printing odd and even numbers

Started by Pro32, March 03, 2005, 01:25:58 AM

Previous topic - Next topic

Pro32

My program sorts the numbers inputed by users and sort it, than prints it. I also want it to print out a row of odd and a row of even numbers. I been told to compare something to 1 but I have no idea. Any help is great. Here is a sample code

; If first element greater 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

roticv

Use test instruction.

Eg

test eax, 1
jz _even

sluggy

Your code doesn't make too much sense because you haven't included enough of it. For instance, what is the bubble flag for? Is it to determine whether to finish the sort early?

Here is some quick psuedo code doing the bubble sort:

  mov ecx, <number of elements to sort>
  lea esi, <name of your array>
  xor endSortFlag, endSortFlag

loopStart:
  test endSortFlag, endSortFlag
  jnz loopEnd
  not endSortFlag   ;invert flag, turn it on

  dec ecx
  jz loopEnd
  mov edx,  <number of elements to sort>

innerLoop:
  dec edx
  jz loopStart

  mov eax, dword ptr [esi + 4]
  cmp eax, dword ptr [esi]
  jge noSwap    ;no swap needed

  ;swap the values:
  push eax
  push dword ptr [esi]
  pop  dword ptr [esi + 4]
  pop  dword ptr [esi]
  xor endSortFlag, endSortFlag  ;turn the flag off, sorting not finished yet

noSwap:
  add esi, 4
  jmp innerLoop


loopEnd:
  ..... write out your array ...

Now there is probably a glaring logic bug in that code, but i have been coding complex stored procedures all day and my brain is fried :dazzled:

To determine if your numbers are odd or even is very simple: if the LSB is set then it is odd. To determine if the LSB is set, use either SHR or ROR, then check the carry flag.


Pro32

#3
I know I didn't include enough code. But I didn't think you needed the whole thing. I was able to sort the numbers and printed out in order. I wassn't able to print out only odd and even. So i didn't know u need the whole code. Sry

Quote from: sluggy on March 03, 2005, 11:43:38 AM
Your code doesn't make too much sense because you haven't included enough of it. For instance, what is the bubble flag for? Is it to determine whether to finish the sort early?

Here is some quick psuedo code doing the bubble sort:

  mov ecx, <number of elements to sort>
  lea esi, <name of your array>
  xor endSortFlag, endSortFlag

loopStart:
  test endSortFlag, endSortFlag
  jnz loopEnd
  not endSortFlag   ;invert flag, turn it on

  dec ecx
  jz loopEnd
  mov edx,  <number of elements to sort>

innerLoop:
  dec edx
  jz loopStart

  mov eax, dword ptr [esi + 4]
  cmp eax, dword ptr [esi]
  jge noSwap    ;no swap needed

  ;swap the values:
  push eax
  push dword ptr [esi]
  pop  dword ptr [esi + 4]
  pop  dword ptr [esi]
  xor endSortFlag, endSortFlag  ;turn the flag off, sorting not finished yet

noSwap:
  add esi, 4
  jmp innerLoop


loopEnd:
  ..... write out your array ...

Now there is probably a glaring logic bug in that code, but i have been coding complex stored procedures all day and my brain is fried :dazzled:

To determine if your numbers are odd or even is very simple: if the LSB is set then it is odd. To determine if the LSB is set, use either SHR or ROR, then check the carry flag.


Pro32

I haven't got that far in class yet so I don't know how to use test.
Quote from: roticv on March 03, 2005, 11:21:06 AM
Use test instruction.

Eg

test eax, 1
jz _even

pbrennick

Pro32,

Quote
I haven't got that far in class yet so I don't know how to use test.

Is this a homework assignment?
Paul

Pro32

This is a lab. But he didn't teach anything. Told us to read chapter and do it. Never showed any example.

Quote from: pbrennick on March 03, 2005, 04:09:34 PM
Pro32,

Quote
I haven't got that far in class yet so I don't know how to use test.

Is this a homework assignment?
Paul


roticv

"test" is just like "and" but without updating the value in the register. It just updates the EFLAG.

pbrennick

Pro32,
I hope you enjoy learning Assembly.
Paul

Pro32

Thanks

Quote from: roticv on March 03, 2005, 05:04:56 PM
"test" is just like "and" but without updating the value in the register. It just updates the EFLAG.

Pro32

I might enjoy it if he was actually teaching it. I'm basically learning assembly on my own.

Quote from: pbrennick on March 03, 2005, 05:38:41 PM
Pro32,
I hope you enjoy learning Assembly.
Paul


Pro32

Here is the sort code I used if anyone want to take a look.


.data
;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]
mov[edi], eax
add edi, 4

add esi, 4
mov dh, 17
mov dl,15


call WriteInt
call CrLf
Loop L4

exit
main ENDP
END main