Incrementing from 0 to 100 in assembly language?

You can't trust what any called procedure does to any of the registers. Either push the registers onto the stack and pop them back off after calling printf or have the increment and end point values held in memory and read/written into registers as you need them.

Up vote 6 down vote favorite 1 share g+ share fb share tw.

This is kinda oddball, but I was poking around with the GNU assembler today (I want to be able to at least read the syntax), and was trying to get this little contrived example of mine to work. Namely I just want to go from 0 to 100, printing out numbers all the while. So a few minutes later I come up with this: # count.

S: print the numbers from 0 to 100. . Text string: .

Asciz "%d\n" . Globl _main _main: movl $0, %eax # The starting point/current value. Movl $100, %ebx # The ending point.

_loop: # Display the current value. Pushl %eax pushl $string call _printf addl $8, %esp # Check against the ending value. Cmpl %eax, %ebx je _end # Increment the current value.

Incl %eax jmp _loop _end: All I get from this is 3 printed over and over again. Like I said, just a little contrived example, so don't worry too much about it, it's not a life or death problem. (The formatting's a little messed up, but nothing major).

Assembly link|improve this question edited Aug 21 '08 at 6:25 asked Aug 21 '08 at 6:20Bernard6,02072748 87% accept rate.

1 xorl %eax, %eax is totally equivalent to movl $0, %eax, and takes 3 bytes less. Just saying. :) – Daniel Kamil Kozar Mar 10 at 0:38.

You can't trust what any called procedure does to any of the registers. Either push the registers onto the stack and pop them back off after calling printf or have the increment and end point values held in memory and read/written into registers as you need them. I hope the following works.

I'm assuming that pushl has an equivalant popl and you can push an extra couple of numbers onto the stack. # count. S: print the numbers from 0 to 100.

. Text string: . Asciz "%d\n" .

Globl _main _main: movl $0, %eax # The starting point/current value. Movl $100, %ebx # The ending point. _loop: # Remember your registers.

Pushl %eax pushl %ebx # Display the current value. Pushl %eax pushl $string call _printf addl $8, %esp # reinstate registers. Popl %ebx popl %eax # Check against the ending value.

Cmpl %eax, %ebx je _end # Increment the current value. Incl %eax jmp _loop _end.

1 btw - pusha and popa will push all of the registers, and pop them all.. I've found that quite handy in the past – warren Nov 18 '08 at 17:08 From notes below. It's important to note that... "@seanyboy, your solution is overkill. All that's needed is to replace eax with some other register like ecx.

" – seanyboy Aug 13 '09 at 7:40 @warren - pusha and popa aren't supported in 64-bit mode. – Daniel Kamil Kozar Mar 10 at 0:37 @Daniel Kozar - never worked in 64-bit mode, but this is good to know :) – warren Mar 12 at 13:52.

Printf should return the number of chars printed, which in this case is two: '0' and '\n'. I think it returns this in eax, and when you increment it, you get 3, which is what you proceed to print. You might be better off using a different register for the counter.

You can safely use registers that are "callee-saved" without having to save them yourself. On x86 these are edi, esi, and ebx; other architectures have more. These are documented in the ABI references: math-atlas.sourceforge.net/devel/assembly.

Well written functions will usually push all the registers onto the stack and then pop them when they're done so that they remain unchanged during the function. The exception would be eax that contains the return value. Library functions like printf are most likely written this way, so I wouldn't do as Wedge suggests: You'll need to do the same for any other variable you have.

Using registers to store local variables is pretty much reserved to architectures with enough registers to support it (e.g. EPIC, amd64, etc.) In fact, from what I know, compilers usually compile functions that way to deal exactly with this issue. @seanyboy, your solution is overkill. All that's needed is to replace eax with some other register like ecx.

Nathan is on the right track. You can't assume that register values will be unmodified after calling a subroutine. In fact, it's best to assume they will be modified, else the subroutine wouldn't be able to do it's work (at least for low register count architectures like x86).

If you want to preserve a value you should store it in memory (e.g. Push it onto the stack and keep track of it's location). You'll need to do the same for any other variable you have. Using registers to store local variables is pretty much reserved to architectures with enough registers to support it (e.g. EPIC, amd64, etc.).

Eax is commonly used for function return values. When you call printf it returns number of characters written in the eax register.

You could rewrite it so that you use registers that aren't suppose to change, for example %ebp. Just make sure you push them onto the stack at the beginning, and pop them off at the end of your routine. # count.

S: print the numbers from 0 to 100. . Text string: .

Asciz "%d\n" . Globl _main _main: push %ecx push %ebp movl $0, %ecx # The starting point/current value. Movl $100, %ebp # The ending point.

_loop: # Display the current value. Pushl %ecx pushl $string call _printf addl $8, %esp # Check against the ending value. Cmpl %ecx, %ebp je _end # Increment the current value.

Incl %ecx jmp _loop _end: pop %ebp pop %ecx.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions