Changing 32-bit int to ASCII in ASM?

Started by
3 comments, last by newOperator 20 years, 1 month ago
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
Advertisement
You could simply call the appropriate C function from your asm code.

Otherwise you just have to bite the bullet and code it yourself.
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.
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..
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!
Bungo!

This topic is closed to new replies.

Advertisement