Go Back
Print 32-bit integer on MARS
If you are using MARS for MIPS and you whant to print unsigned integer larger then 2GB
you simply cannot use syscall #1 because this syscall print only signed integer.
For example if you try to print 0xFFFFFFFF as unsigned you will get -1
because the MSB is 1 and it's like 1 with signed extension.

You can use the procedure below to override this problem:
print_integer:
	# $t0 - Store the number
	# $t1 - Store the remain
	# $t2 - Index to store the number length to free the $sp at the end
	# $t3 - Contain 0x0a for division

	li $t2, 0
	li $t3, 0x0a
	move $t0, $a0	
	addi, $sp, $sp, -4
	sw $t2, 0($sp)
	print_integer_loop:
		beq $t0, $zero, print_integer_end
		divu $t0, $t3
		mflo $t0
		mfhi $t1
		addiu $t1, $t1, 0x30	# Translate the number to its ascii code
		addi, $sp, $sp, -4
		sw $t1, 0($sp)
		addi $t2, $t2, 1
		j print_integer_loop

	print_integer_end:
		la $a0, 0($sp)
		li $v0, 4
		syscall
		addi $t2, $t2, -1
		addi, $sp, $sp, 4
		bne $t2, $zero, print_integer_end	
		addi, $sp, $sp, 4 		# Free the zero from the start
		
		jr $ra
All rights reserved to Tovi Levis. DO NOT copy or redistribute any content from this site.
Close