Hello NASM forums,
( I just tried to post this and the post button wasn't working, so I am retyping it )
Basically, I have a program I am writing that I want to print out integers to STDOUT. I have researched many ways of doing this, some used 16-bit registers, some were written in TASM/MASM, some used 64-bit registers, and some used 32-bit registers but were very opaque. I finally understood what was being done, and wrote a modified version of the itoa code from Bare Metal OS.
For trouble-shooting purposes, I stripped out the itoa code I wrote and put it into a separate .asm to try and figure out where I was going wrong. Here it is:
section .text
global _start
_start:
mov eax, 1231
mov ebx, 10
xor ecx, ecx
;; Now is the main loop of the conversion. It takes each
;; digit off of the input starting at the ONES place and
;; moving upward until it reaches the end of the number
godivide:
xor edx, edx ; clear remainder
div ebx ; divide eax by 10
push edx ; put edx on stack
inc ecx ; increment number of digits
cmp eax, 0 ; if eax
jne godivide ; is not equal to zero, divide again
;; Next we have to go through each digit and add 0x30 ('0')
;; to each digit so that it will print correctly
;; For simplicity, each digit is written as it is converted
nextdigit:
pop eax ;pull off the stack
add eax, 30h ;add '0' to eax
push ecx ; store counter
xor ecx, ecx
;; write the value to STDOUT
mov edx, 1 ; one byte
mov ecx, eax ; char
mov ebx, 1 ; stdout
mov eax, 4 ; syswrite
int 80h
;;
pop ecx ; retrieve counter
dec ecx ; move down through digits
cmp ecx, 0 ; if ecx
jg nextdigit ; is greater than 0, jump nextdigit
mov eax, 1
mov ebx, 0
int 80h
Compiling with
$ nasm -g -f elf32 itoa-test.asm; ld -o -melf_i386 itoa-test itoa-test.o
Before the method above, where I'm trying to print each character as I convert it, I was trying to use stosb and lodsb, but I don't understand how they work exactly when I'm not calling a function and getting it back (and if I did that, I don't know how to get the char bytes back). I want to be able to call this as a macro or something so that I can just do a single line in the code for printing the integer to the screen (in line). I have yet to be able to get this to work.
I can watch the registers using gdb and see the correct conversion take place. What I do not see is the printing occur like it should. Does this have to do with forcing i386 architecture? My machine is x86_x64 but the code must be 32bit.
I'm new to NASM (just started last week) and I've spent what feels like ages (but is in reality only like 10-12 hours) researching how to do this. Any advice or tips on this are welcome.
Thanks for your time,
Nick