943,585 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Marked Solved
  • Views: 2038
  • Assembly RSS
Nov 10th, 2008
0

learning nasm assembly, need some help

Expand Post »
Hello!

I've started learning assembly and I have some problems with a program. I'm trying to write a program that would print some text x-times (x would be inputed by me), but I always get an infinite loop, because it seems that the counter value doesn't get updated in the register in each iteration, therefore it never reaches the "x".

My code

Assembly Syntax (Toggle Plain Text)
  1. BITS 32
  2. extern printf
  3. extern scanf
  4. global main
  5.  
  6. section .data
  7.  
  8. text db "Random text",10,0
  9. input db "%d",10,0
  10. number dd 0
  11.  
  12. section .text
  13.  
  14. main:
  15.  
  16. push dword number
  17. push dword input
  18. call scanf
  19. add esp,8
  20. mov eax,[number]
  21. mov ecx,1
  22.  
  23. .loop:
  24.  
  25. push dword text
  26. call printf
  27. add esp,4
  28. inc ecx
  29. cmp ecx,eax
  30. jle .loop
  31.  
  32. ret

Any help would be much appriciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
flash121 is offline Offline
49 posts
since Jan 2008
Nov 10th, 2008
0

Re: learning nasm assembly, need some help

I'd be willing to bet that printf changes the eax register... Have you looked into that?
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008
Nov 11th, 2008
0

Re: learning nasm assembly, need some help

You're probably right, because i tried to increment the value in it twice in a row outside the loop, withouth using printf before. I only used it after and the value was indeed incremented twice like it was supposed to be.

Also, eax getting messed up isn't the only problem in my code, ecx doesn't get incremented either.
EDIT: well actually it does get, but it resets back after the code jumps back to the start of the loop
Last edited by flash121; Nov 11th, 2008 at 9:45 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
flash121 is offline Offline
49 posts
since Jan 2008
Nov 11th, 2008
0

Re: learning nasm assembly, need some help

Managed to solve it. Printf was indeed messing with the values in the register, so a simple pushad before the function call and a popad after solved it.
Last edited by flash121; Nov 11th, 2008 at 4:39 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
flash121 is offline Offline
49 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: need help generating graphic
Next Thread in Assembly Forum Timeline: Data forwarding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC