Hello, again!

Thank you. I think I have two issues that I have to solve now in my code.
1. getchar - INBUFFERT.
I do not understand  how to get the position in the buffer. Look at the code:

getinpos:
movl START_INPOS, %ecx
movl %ecx, %ebx

getchar:
movl $INBUFFERT, %ebx
xorl %eax, %eax
/*cmpb $45, (%ebx)
je increase_charN */

getchar_loop:
cmpb $32, (%ebx)
je increase_char
movsx (%ebx), %eax
pushl %eax
incl %ecx
incl %ebx
jmp getchar_loop

increase_char:
incl %ecx
movl %ecx, END_INPOS
decl %ecx


Reading in the first number works fine.
Ex; 314SPC555CR
So, the program takes 314 and converts it and so on....
Everything works fine til the next time I come to getchar. So, coming to 555 in INBUFFERT, i e position = 4(START_INPOS,  i e %eax = 4, %ebx =4). Now I want to move the number 5 into %eax. But I get 3, i e the position in the buffer is 0. ? How do I get to move number 5 in position 4?
Or, in other words, coming back to catch number 5 in position 4, why isn't this working?

The clearing of the buffer UTBUFFERT, I don't know if this is  the correct code?

outimage:
pushl $UTBUFFERT
                call puts
addl $4, %esp
                movl $BUFFSIZ, %ecx
                movl $UTBUFFERT, %edx
clearloop:
                movb $0, (%edx)
                incl %edx
                loop clearloop
ret

I can't get this to work. Isn't this an endless loop?

Anders
Posted on 2011-10-11 09:10:10 by anders11
Let's deal with the last question first, since it's a little simpler(?)...


outimage:
pushl $UTBUFFERT
                call puts
addl $4, %esp
                movl $BUFFSIZ, %ecx
                movl $UTBUFFERT, %edx
clearloop:
                movb $0, (%edx)
                incl %edx
                loop clearloop
ret

That should work, I think. To make clear what the "loop" instruction does, here's an alternative which does the same thing...

outimage:
pushl $UTBUFFERT
                call puts
addl $4, %esp
                movl $BUFFSIZ, %ecx
                movl $UTBUFFERT, %edx
clearloop:
                movb $0, (%edx)
                incl %edx
                decl %ecx
                jnz clearloop
ret

There's a possible "gotcha" here - one of the many mistakes I made in "my version" - C library functions are allowed to alter %ecx. I did something like...

...
movl $5, %ecx
top:
push %eax
call putchar
addl $4, %esp
loop top
...

That was an "infinite loop", 'cause the call to putchar altered %ecx! I had to:

...
movl $5, %ecx
top:
pushl %ecx
push %eax
call putchar
addl $4, %esp
popl %ecx
loop top
...

The "loop" instruction works with %ecx. If I'd used some other register as a loop counter, I could have done:

...
movl $5, %ebx
top:
push %eax
call putchar
addl $4, %esp
decl %ebx
jnz top
loop top
...

But you don't do that in your code, so it "should" have worked either with "loop" or the explicit "decl/ jnz". Note that "decl" sets (or clears) the zero flag, so we don't need to compare the register to zero (doesn't hurt, but it's "useless code", which we'd like to avoid).

I said that was a "simpler" question, but I don't know the answer - "should" work.

That brings us to the more complicated question...

getinpos:
movl START_INPOS, %ecx
# whatever was in START_INPOS is in %ecx
movl %ecx, %ebx
# make a copy of it
getchar:
movl $INBUFFERT, %ebx
# now %ebx is overwritten with the address of INBUFFERT.
# perhaps you wanted to do "addl" here?
xorl %eax, %eax
# ?
/*cmpb $45, (%ebx)
je increase_charN */
# you were going to check for '-' here
# and jump off into the "bowl of spaghetti"...
# perhaps

# cmpb $'-', (%ebx)
# jne getchar_loop
# incl %ebx
# maybe set a "minus indicator" of some sort?
getchar_loop:
cmpb $32, (%ebx)
je increase_char
# maybe, instead:
# jne notspace
# incl %ebx
# movl %ebx, NEXT_POS ?
# save the current position (address)
# we have a complete number,
# do something with it
# jmp convert_loop?
# %ebx is neither a '-' nor a ' '...
# must be a number (character)!
# notspace:
movsx (%ebx), %eax
# get a character(?)
# you might want movb (%ebx), %al ?
pushl %eax
# push it to be popped off in "conv_loop"?
incl %ecx
# bump "small integer" POS
incl %ebx
# bump the address
jmp getchar_loop

increase_char:
incl %ecx
movl %ecx, END_INPOS
decl %ecx
# where do we go from here?

I guess what you're doing is keeping a "small integer" in "?POS". Okay, but you'll want to add it to the address of the start of the buffer before you can do much with it. It might be easier to make these "?POS" variables the actual "current address in buffer" instead?

Another nice day here - I thought yesterday was the last one. That's all I can think of now, but "I'll be back!". :)

Best,
Frank

Posted on 2011-10-11 15:48:42 by fbkotler
I've managed to solve most of my code now. Thank you for helping me out.

Anders
Posted on 2011-11-10 04:29:07 by anders11