Page 1 of 1

[asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 4:48 am
by hyperzap
Hi all,

I am relatively new to OSdev, and am building a very simple monotasking OS, but Ive been neglecting this for a while and I need a way to print numbers to the screen.

So I tried writing this code.

Code: Select all

;===============================================================
;		PRINT INT TO STRING
;	CONVERTS AN INTEGER TO A STRING FOR PRINTING
;
;		GIVE INT in EAX
;	     Prints a number to screen
;
;===============================================================
Print_Int_to_str:
  pusha
  mov dword [.num], eax
  mov dword [.temp], 0

.startloop:
  div eax, 10	;<-- This is what fails :(
  cmp eax, 0       ;I am making the assumption that eax holds the result, edx the remainder
  je .end

  push eax
  add edx, 30      ;Makes the number in the numeric range move to num in ascii range
  mov dword [.output], edx
  mov dword [.output+1], 0

  mov ebx, .output
  call PutChar32	;String ptr in ebx, this prints to screen.

  pop eax
  jmp .startloop

.end:
  popa
  ret

.num dd 0
.temp dd 0
.output db 0, 0
I have never used div in assembly before, so I have no idea what it entails. It is this instruction that gives me the error:
Error: Invalid combination of opcode and operands.
This is under NASM on win XP btw.


Thankyou very very very much in advance.

-Hyperzap

Re: [asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 5:15 am
by Andr3w
hyperzap wrote: I have never used div in assembly before, so I have no idea what it entails. It is this instruction that gives me the error:
Error: Invalid combination of opcode and operands.
This is under NASM on win XP btw.
Syntax of div instruction is:

Code: Select all

div syntax
-- Andrew

EDIT: http://wiki.osdev.org/Getting_Started#R ... _Knowledge recommends (requires?) you to have knowledge about the low-level language Assembly.

Re: [asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 5:23 am
by Brendan
Hi,

For DIV the dividend is implied and is always "rdx:rax" (128-bit dividend), "edx:eax" (64-bit dividend), "dx:ax" (32-bit dividend) or "ax" (16-bit dividend). It also doesn't support an immediate divisor - you have to divide by a register (e.g. "div ebx") or divide by the value at an address ("e.g. "div dword [foo]"). Also, the divisor must be half the width of the dividend (e.g. you can't divide a 64-bit number by a 64-bit number or by a 16-bit number, and have to divide by a 32-bit number).

Something like this would divide properly:

Code: Select all

    mov edx,0          ;eax:edx = dividend
    mov ebx,10         ;ebx = divisor
    div ebx            ;eax = quotient, edx = remainder
Once that's fixed, I think your "int to string" will do things backwards - the number 1234 will become the string "4321".


Cheers,

Brendan

Re: [asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 7:11 am
by hyperzap
Brendan wrote:Hi,

For DIV the dividend is implied and is always "rdx:rax" (128-bit dividend), "edx:eax" (64-bit dividend), "dx:ax" (32-bit dividend) or "ax" (16-bit dividend). It also doesn't support an immediate divisor - you have to divide by a register (e.g. "div ebx") or divide by the value at an address ("e.g. "div dword [foo]"). Also, the divisor must be half the width of the dividend (e.g. you can't divide a 64-bit number by a 64-bit number or by a 16-bit number, and have to divide by a 32-bit number).

Something like this would divide properly:

Code: Select all

    mov edx,0          ;eax:edx = dividend
    mov ebx,10         ;ebx = divisor
    div ebx            ;eax = quotient, edx = remainder
Once that's fixed, I think your "int to string" will do things backwards - the number 1234 will become the string "4321".


Cheers,

Brendan
Thankyou. Thats exactly what I needed.

-Hyperzap

Re: [asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 8:10 am
by Chandra
I use a slightly different technique for division. But as a whole, the theory is same.
Here's what I do,

Code: Select all

XOR EDX,EDX ;clear EDX
;place number to be divided into EDX (32-bit)
XOR CX,CX ;clear CX
;place 16-bit denomenator into CX
mov AX,DX ;move least significant bits to AX
shr EDX,16 ;shift most significant bits to DX
div CX ;[b]divide DX:AX by CX[/b]
;quotient in AX
;remainder in DX
It seems as if Brendan's technique is concise than mine.

Regards.

Re: [asm] My code isnt working: Converting int to string

Posted: Mon May 30, 2011 9:09 am
by Love4Boobies
If you're not in long mode, you might also want to consider AAM, which is a whole lot faster.