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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
hyperzap
Posts: 5
Joined: Tue May 17, 2011 6:59 am

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

Post 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
User avatar
Andr3w
Member
Member
Posts: 76
Joined: Tue Jun 09, 2009 4:09 am
Location: Somewhere

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

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
hyperzap
Posts: 5
Joined: Tue May 17, 2011 6:59 am

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

Post 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
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

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

Post 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.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post by Love4Boobies »

If you're not in long mode, you might also want to consider AAM, which is a whole lot faster.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply