• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Changing 32-bit int to ASCII in ASM?

## 4 posts in this topic

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
0

##### Share on other sites
You could simply call the appropriate C function from your asm code.

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

##### Share on other sites
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.
0

##### Share on other sites
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..
0

##### Share on other sites
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	mainmain:	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	syscallitoa:	li	$t0,'' '' bgez$a0,itoa_start	li	$t0,''-'' neg$a0,$a0itoa_start: sb$t0,0($a1) addi$a1,$a1,1 move$t0,$a1itoa_loop_start: div$a0,$a2 mflo$a0	mfhi	$t1 blt$t1,10,itoa_less_than_10	b	itoa_greater_than_10itoa_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_reverseitoa_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_reverseitoa_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!
0