0
votes

For example, you have a string and a buffer:

text: .asciiz "111100010"
buffer: .space 1000

How do you convert each number to an ascii character and store them in a buffer?

i.e buffer should have this content: (ascii code of "1" is 49, "0" is 48)

494949494848484948

I know how to print them out individually since I can use syscall code 1 to interpret them as integers

main:
    la $a0,text
    j print
    
    print_done:
    
    li $v0,10
    syscall
    
print:  move $s0,$a0  #save addr 
print_loop:

    #addi $s0,$s0,20
    
    lb $s1,($s0)
    beqz $s1,print_done    # check end of string  
    li $v0,1        # print as int
    move $a0,$s1
    syscall

    addi $s0,$s0,1      # move forward
    j print_loop

But these characters are printed out separately. How can I put them all in the buffer and print them all together?

Thank you!

1
If you only have 0 or 1, you can simply iterate the string and put 48 or 49 as you see a 0 or 1. What is causing you problem? - Jester
Yes but I'll have to check if it's a 0 or 1 everytime. since "0" is just 48, is there a way to directly put ascii code of "0" into buffer? - wedeservehappiness

1 Answers

0
votes

MARS has a rich set of syscalls.  You can print a number to the console as decimal (signed or unsigned), as hex, as binary, etc..

However, as rich as the capabilities are, they are by no means a complete programming model.  These varied printing forms can only print to the console — there is no version of these that will print to a file or print to a string buffer in memory.

If you want to print decimal numbers to a buffer, you'll have to program it, to print character by character.  In your case the job is relatively simple if there's only a choice of two characters for the input.

Your input as a string "101" will become output as string "494849".  For each character in the input string, you add two characters to the buffer.  If an input character is '1' then add '4' followed by '9' to the buffer, and otherwise add '4' followed by '8' to the buffer.  Append a trailing null, and you can then print the memory buffer as a string.