Asaf
Asaf

Reputation: 129

Trying to understand the SYSCALL 5 example, the order of the literals % placeholder is confusing

I'm learning MIPS64 and using EduMIPS64 simulator.

I understand the instructions of the following example, I tried to execute it cycle after cycle but I don't get how the compiler knows which number or string matches to corresponding placeholder and how all related to format_str so in the end of the .code section, it's enough to put the address of format_str in r14

I know that System calls expect that the address of their parameters is stored in register R14, but how all the others relate to this address(format_str)?

For each %s, %d or %i placeholder, SYSCALL 5 expects a parameter, starting from the address of the previous one. When the SYSCALL finds a placeholder for an integer parameter, it expects that the corresponding parameter is an integer value, when if it finds a placeholder for a string parameter, it expects as a parameter the address of the string.

I tried understand it by the memory representation with no success.

            .data
format_str: .asciiz "%dth of %s:\n%s version %i.%i is being tested!"
s1:         .asciiz "June"
s2:         .asciiz "EduMIPS64"
fs_addr:    .space  4
            .word   5
s1_addr:    .space  4
s2_addr:    .space  4
            .word   0
            .word   5
test:
            .code
            daddi   r5, r0, format_str
            sw      r5, fs_addr(r0)
            daddi   r2, r0, s1
            daddi   r3, r0, s2
            sd      r2, s1_addr(r0)
            sd      r3, s2_addr(r0)
            daddi   r14, r0, fs_addr
            syscall 5
            syscall 0

Thanks.

Upvotes: 0

Views: 286

Answers (1)

Michael
Michael

Reputation: 58487

The simulator doesn't really need to know anything about placeholders.

It knows where the format string and all the other values are located in the simulated memory (fs_addr), because you passed that address in r14. So the simulator could just take that address and map it to the corresponding address in the host machine's memory, cast the first two words at that address into a const char* and a va_list, and then call vprintf.

I don't know if that's what EduMIPS64 actually does, but that seems like one of the simpler solutions.


This shows what each of the placeholders in your example correspond to:

"%dth of %s:\n%s version %i.%i is being tested!"
 |       |    |           |  | 
 |       |    |           |  +-+
 |       |    |           +-+  |
 |       |    +----------+  |  |
 |       +-------------+ |  |  |
 +--------->.word   5  | |  |  | 
                       | |  |  |
s1_addr:    .space  4<-+ |  |  |
s2_addr:    .space  4<---+  |  |
            .word   0<------+  |
            .word   5<---------+

Upvotes: 2

Related Questions