简体   繁体   中英

MIPS assembly, .space data

For a homework assignment. I trying to print out a .space 11 data using a loop and loading each byte using

lb $a0, data($t9)

do i have to load each byte to print a. space item or is there a better way?

loop2:
        lbu $t9, newNum($t8)
        
        li $v0, 11
            move $a0, $t9
            syscall
                
        addi $t8, $t8, 1
            lbu $t9, newNum($t8)
            bnez $t9, loop2
        

side note: is there another way to detect the end of a .space ?

Thank you

do i have to load each byte to print a.space item or is there a better way?

If you format the space as a C string, you can use "print string" syscall, 4. Formatting as a C string means having a final null character, which it looks like you have already.

BTW, your loop assumes that the string has at least one character (its length >= 1), which is not true in general (some strings have length 0), but may be so in your program.

side note: is there another way to detect the end of a.space?

It depends on how you're using the space. If it is a fixed buffer for a compile time constant string, you can take the difference between two labels:

myspace:    .space 11
myspaceEnd:

...
la $t0, myspaceEnd
la $t1, myspace
sub $t3, $t0, $t1

This will place the size of the.space into $t3 . Some but not all assemblers allow a constant expression, such as myspaceEnd-myspace , since this is a compile constant, it would translate into a runtime constant immediate value, and we could forgo the runtime computation (the above 3 instructions) of the compile time constant.

However, these compute the static/compile-time size of the space. If you use the space as a buffer: at runtime place a string there whose length is only known dynamically/at-run-time, this won't compute that dynamic length, it will only give the static length of the space (ie the max allowed length).

Chances are you are using the space as a buffer for a runtime string that could be any length, since the .space directive does not allow for initialization — whereas the .asciiz and .byte directives do.

MARS & QtSpim allow reading a string from the console or from a file. When reading a string from the console, the syscall does not give you the length of the string, so strlen -type of operation is necessary to find the length of such string. The best we can do there is compute the length when needed, and store that length if we need it in several places in the program.

When reading from a file, the syscall does return a length, so there's no need for the strlen operation, if the length return by the syscall is preserved for when needed by the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM