0

This is only my first week of learning assmebly language and I am trying to write a program that counts from the value in eax to the value in ebx.

This is my code so far. From what I understand I am making room for two long values in the x section and room for the sum of the count. Than I insert 5 into eax and 10 into ebx and the value of x which will be count into ecx. I than compare eax to ebx and increment eax and jump if not zero. Once the count is zero I move ecx into the sum.

But when I run the program when I print sum I get 0 or some other number. The number I'm excepting is 5 since 5 to 10 is 5.

Any ideas?

.data # The data section
x:
    .long 1
    .long 2
sum:
    .long 0
.text # The text section

.globl _start

_start:
    movl $5, %eax
    movl $10, %ebx
    movl $x, %ecx
top:
    cmpl %eax, %ebx
    incl %eax
    jnz top
done:
    movl %ecx, sum
burnsi
  • 6,194
  • 13
  • 17
  • 27
  • 3
    `INC` modifies the `Z` flag, so you're jumping based on the `INC`, not the `CMP`. – Michael Jul 03 '22 at 06:50
  • 2
    Also, you're never writing to `ecx` anywhere after that `movl $x, %ecx`. If all you wanted to do was set it to `ebx-eax` then a simple subtraction would do; no need for any loop. – Michael Jul 03 '22 at 06:59
  • 1
    What do you mean "count value"? Are you just trying to loop EAX over the `[5 .. 10)` range, like `for (int i=5 ; i<10 ; i++)`? What does `x[]` have to do with that; it seems irrelevant to an [mcve] of your looping problem? That range of EAX values wouldn't work as indices to that array. You're doing `sum = &x[0]`, i.e. a pointer to the first element, having nothing to do with your loop. – Peter Cordes Jul 03 '22 at 07:07
  • If you want the compiler to make an example for you, see [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) (especially Matt Godbolt's video). Use `gcc -m32 -fno-pie -Og` for a C function that does what you want with a global array. – Peter Cordes Jul 03 '22 at 07:09

0 Answers0