Hi,
I am trying to write a program to convert an integer to a character.
Below is my code. Can someone tell me what I am doing wrong?
It is for the MIPS chip.

thank you,
Michael

# A test to print charactors from numbers.
# Does not work yet. Can't get it to print the ASCII equivilant char to the number.

        .data
prompt: .asciiz "\nType an integer: "
bye:    .asciiz "\nThe end :)\n\n"


        .text
main:    la $a0, prompt
        li $v0, 4
        syscall
        li $v0, 5
        syscall
        move $a0, $v0
        li $v0, 1
        syscall
        j end
        
end:    li $v0, 4
        la $a0, bye
        syscall
        li $v0, 10
        syscall

sorry I don't know MIPS assembly, but if a register contains a single digit then simply add '0' to it

mov ax,5 ; a numeric digit in register ax
add ax,'0'   ; make the digit printable
;; now print the digit in register ax

Thank you for trying.
I added a zero with

addi $a0, $a0, 0

and

addi $a0, $a0, '0'

neither worked, assuming I did it right to begin with.
I forgot to say in the first post that when a number is entered, it prints "(null)".
It still does with the added zero.

your second version with the quotes around the zero is the correct version. If you look at any ascii chart (just google for it) you will see that '0' is printable, but 0 is not.

my guess is that $a0 does not contain what you think it contains. To check, I think I would just simply hard-code something in the register just to get it to work.

Is there any online documentation for those syscall's, like 4 means print a string.

Check that 5 (which I presume means 'read') does what you think it should do.
My guess is you should be passing a buffer and a length to it.

Salem: I am reading a book "MIPS assembly language programming" by Britton. I think I have got the syscalls right. All the numbers check out. Of course, I could be missing something.

Ancient Dragon: I feel kinda dumb saying this, but, I don't see a section 4.2.1. Nice PDF you found though. Though I don't have time to investigate tonight, maybe the "atoi" program can teach me what I need to know.

This article has been dead for over six months. Start a new discussion instead.