Jump to content

  • Log In with Google      Sign In   
  • Create Account


Changing 32-bit int to ASCII in ASM?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 newOperator   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 April 2004 - 08:36 AM

I have a new appreciation for computers now, considerring what I took for granted before, but maybe there is an easy solution to the following: How would some one change an integer to an ascii string representation of the integer in base ten, of course. It seems easy enough to do with small numbers, but not so easy with larger numbers, for some reason... I don''t know. So for instance taking 1110 1110 and changing it to the appropriate bytes (oh man, maybe that''s it) but 238... I still am not sure. Thank-ya

Sponsor:

#2 Willm   Members   -  Reputation: 138

Like
Likes
Like

Posted 03 April 2004 - 08:45 AM

You could simply call the appropriate C function from your asm code.

Otherwise you just have to bite the bullet and code it yourself.

#3 newOperator   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 April 2004 - 08:50 AM

Actually I want to code it myself -- I''m reading Elite Programmer''s Delight with tons of bit manipulations in it, and tons of simple ways to do the coolest things, mostly in assembler. But I can''t find anything on how to change an integer to an ascii representation.

#4 Willm   Members   -  Reputation: 138

Like
Likes
Like

Posted 03 April 2004 - 08:55 AM

quote:
Original post by newOperator
Actually I want to code it myself -- I''m reading Elite Programmer''s Delight with tons of bit manipulations in it, and tons of simple ways to do the coolest things, mostly in assembler. But I can''t find anything on how to change an integer to an ascii representation.

Fair enough. I usualy do things in C then convert it to asm - to make sure the algorithm is working.

You could calculate the modulus of 10 to get the least significant digit, then divide by 10 and repeat the process. Of course, that gives you the digits in reverse order - so you''ll have to figure your way around that little problem..

#5 BungoMan85   Members   -  Reputation: 158

Like
Likes
Like

Posted 03 April 2004 - 11:37 PM

well its not x86 asm which is prolly what you want. but i actualy had to write an integer to hexadecimal (ascii) program for my digital systems class the other day. unfortunatly its RISC asm for a MIPS processor... here is a slightly modified version that takes a 32 bit word, a buffer, and a radix and converts the word into an ascii string of the right radix.


.data
prompt1: .asciiz "Enter a number: "
prompt2: .asciiz "Enter the base to convert to: "
result: .asciiz "The converted number is: "
endl: .asciiz "\n"
buffer: .space 80
number: .word 0
radix: .word 0

.text
.globl main
main:
la $a0,prompt1
li $v0,4
syscall

li $v0,5
syscall

sw $v0,number

la $a0,prompt2
li $v0,4
syscall

li $v0,5
syscall

sw $v0,radix

lw $a0,number
la $a1,buffer
lw $a2,radix
jal itoa

la $a0,result
li $v0,4
syscall

la $a0,buffer
li $v0,4
syscall

la $a0,endl
li $v0,4
syscall

li $v0, 10
syscall

itoa:
li $t0,'' ''
bgez $a0,itoa_start
li $t0,''-''
neg $a0,$a0
itoa_start:
sb $t0,0($a1)
addi $a1,$a1,1
move $t0,$a1
itoa_loop_start:
div $a0,$a2
mflo $a0
mfhi $t1
blt $t1,10,itoa_less_than_10
b itoa_greater_than_10
itoa_less_than_10:
addi $t1,$t1,''0''
sb $t1,0($t0)
addi $t0,$t0,1
bnez $a0,itoa_loop_start
sb $zero,0($t0)
b itoa_reverse
itoa_greater_than_10:
addi $t1,$t1,87
sb $t1,0($t0)
addi $t0,$t0,1
bnez $a0,itoa_loop_start
sb $zero,0($t0)
itoa_reverse:
addi $t0,$t0,-1
ble $t0,$a1,itoa_end
lbu $t2,0($a1)
lbu $t3,0($t0)
sb $t2,0($t0)
sb $t3,0($a1)
addi $a1,$a1,1
b itoa_reverse
itoa_end:
addi $a1,$a1,1
jr $ra
.end


i guess you could convert that to x86 asm. i dont know x86 asm like the back of my hand yet so itd take me a long time to do it.

my instructor had the c source code, if i can find it ill post it. its pretty neat.

Bungo!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS