0

I have an array that I am attempting to print. I would like to print it out so I can see if it is correct. It is currently printing the number 1 and stopping. Or, if I mess with the ECX differently it prints out a bunch of zeros and crashes.

Here is my program.

.data

array DWORD 10 DUP(5, 7, 6, 1, 4, 3, 9, 2, 10, 8)
my_size dd 10

sorted DWORD 0
first DWORD 0
second DWORD 0

.code

start:
main proc
cls

 mov EBX, offset[array]
 mov ECX, [my_size]
 dec ECX
 sub ESI, ESI
 sub EDI, EDI

; print
mov EBX, offset aa
sub ECX, ECX
;mov ECX, my_size
mov ECX, 10

my_loop:
mov EAX, [EBX]
inc EBX
dec ECX

cmp ECX, 0
jle exit_loop

mov first, EAX
print chr$("printing array elements: ")
print str$(first)

loop  my_loop

exit_loop:
ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    Using assembly to write an O(n^2) sort algorithm makes *zero* sense. Use the [homework] tag for homework questions. – Hans Passant Jul 23 '12 at 19:37

2 Answers2

2

I hate to say it, but you're not "ready" to write a bubble sort. Either it's a completely insane homework assignment, or you haven't followed along with the class so far (possibly both).

Very first thing, I don't think you've defined your array correctly. As I read your code, you've got 100 dwords there - 10 copies of the 10 numbers you specify. You shouldn't need "DUP" in there.

I would print the unsorted array first, just to make sure you've got that part right. You appear to be using a couple of macros, there - they sure as heck aren't instructions. Just from the names, I would guess(!) that "print_chr$" prints a single character and "print_str$" prints a string (although you seem to be printing your string and the number 1). If you've got a "print_int$" in your macro set, I would guess(!) that's what you want. Since I'm not familiar with your macros, I could be wrong.

Although you've defined the array as "dword", you only compare a single byte in your sort routine. This probably works for the small numbers you're using, but it isn't really right.

The usual way to do a bubble sort is to set a "flag" (register or variable - this may be what "sorted" is for) to zero at the beginning of each run through the array, and set it to 1 every time you do a swap. When you've done a pass through your array and the flag is still zero - you haven't done a swap - then, and only then, your array is sorted. If you print the array after each pass, you'll see why it's called a "bubble" sort - the smallest/largest number "bubbles up" to its final position.

Your code to walk through a dword array (esi * 4) looks about right (outside of only comparing a byte), but your print routine only increments ebx by one each time through the loop. Either "add ebx, 4" or use "ebx * 4" (not both) to print dwords. Or perhaps your array is only supposed to be bytes?

Seriously, I'd start with something simpler - just print the array - and work up to adding the sort routine after you've got that working.

Hope it helps.

Best, Frank

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4
  • Your code to walk through a dword array (esi * 4) looks about right (outside of only comparing a byte), should i be comparing to 16bit register instead? –  Jul 24 '12 at 23:28
  • Might as well go with the full 32 bits, no? – Frank Kotler Jul 25 '12 at 18:25
  • Dont think i have any 32bit registers to use that i am not already using to replace ah, and al for storing index for comparing. –  Jul 26 '12 at 00:44
  • edx doesn't look busy. ebp is "usually" used as a stack frome pointer, but can be used as a "general purpose" register (since you don't seem to have a stack frame). – Frank Kotler Jul 26 '12 at 06:12
0

I see you've simplified the code. Good idea! I'm still not familiar with the macro you're using, "print_str$". That doesn't "look" to me like it prints a number. Have you got a "print_int$" or similar? If you can get it to print just the first number "5", that would be a good start.

Now... working through your loop, you just "inc ebx". That won't get you the next dword, it'll get you bytes 2, 3, and 4 from the first dword and the first byte from the second dword. Since you used "* 4" in the (removed) sort code, you probably want "[ebx * 4]" here. Either that, or add 4 to ebx each time through the loop. One or the other (but not both) should step through an array of dwords.

I suspect that the first step would be to select the "right" macro to print a number. It'll probably get easier from there(?). Courage! :)

Best, Frank

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4