I'm checkin out asm development (and x86 asm in general) on Linux and while writing this hello world type program I run into a question of how to determine the length for int 80 functions 3 and 4 (especially for 3) when I want to read user input and then write it back to stdout. Also I cant seem to zero out my input variables/lables/memory locations (what is the correct term?) by xoring... And part of the previous input gets strangely printed on next inputs...
Thanks for your help! Hard work on filling the basic knowledge requirements for kernel dev =D
It's better to use the term "system call" rather than "interrupt" for these functions. 3 and 4 are the system calls sys_read and sys_write. You can find full details of these system calls by Googling them. In answer to your specific question, ecx contains the address of a memory buffer that you have allocated, and edx the length of this buffer. In your case you allocated 10 bytes for the input buffer, so edx should be 10. When the function returns it will tell you how many bytes were actually read (10 or less) in eax.
To zero out any variables you just move 0 into them. Don't do that with user_input - that would zero the address of the buffer, which is not what you want. Just move a 0 into each of the 10 bytes of the buffer. Everyone seems to be hung up on using the xor trick to zero a register; I prefer to do an explicit move. It hardly uses any more memory or clock cycles and is clearer IMO; we don't live in the days of 16K RAM and 4.7MHz processors so don't have to get too precious about these things.
There are several good books and online tutorials about assembler programming. Read one or more of these rather than just trying to make sense of odd snippets of assembler code.
Thanks! I do have a few books I'm reading and the nice thing is that actually no snippets were copied on this one... Which is just the result of iteration on these things (reading/copy pasting) (and finally I feel like I'm getting a grasp on this)
Gonna try out your hint.
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
One thing I should have added, and most strongly recommend, is to run your programs under a debugger (such as gdb on Linux). That way you can single-step through code and see exactly what is happening to registers and memory locations. It takes a little effort to learn how to use the debugger, but it's well worth it.
I'm checkin out asm development (and x86 asm in general) on Linux and while writing this hello world type program I run into a question of how to determine the length for int 80 functions 3 and 4 (especially for 3) when I want to read user input and then write it back to stdout. Also I cant seem to zero out my input variables/lables/memory locations (what is the correct term?) by xoring... And part of the previous input gets strangely printed on next inputs...
That's because you aren't clearing your input string, you're just loading its address into eax and then setting eax to 0 with the xor.
What you'd want to do is set eax to zero, edi to the address of your input string, ecx to the length of your input string, and use rep stosb to zero it.
iansjack wrote:To zero out any variables you just move 0 into them. Don't do that with user_input - that would zero the address of the buffer, which is not what you want. Just move a 0 into each of the 10 bytes of the buffer.
This is not going to make things less confusing. The label user_input is not pointing to a pointer of the buffer. The label value cannot be changed at run-time.
It is important to know how to zero buffers. However, in this case it is not needed. You have to know how many characters you get from the user and use that information for determining how many characters in the buffer are valid. Things will become clear very soon! Keep going.