0

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.

Andrea Spadaccini
  • 12,378
  • 5
  • 40
  • 54
Asaf
  • 107
  • 1
  • 12

1 Answers1

2

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<---------+
Michael
  • 57,169
  • 9
  • 80
  • 125
  • For example, if change the data from `fs_addr: .space 4 .word 5` s1_addr: .space 4 s2_addr: .space 4` to: `fs_addr: .space 4 `s1_addr: .space 4 .word 5 s2_addr: .space 4` I replaced the order of '.word 5` and `s1_addr: .space 4` so the print is: `48th of 5: EduMIPS64 version 0.5 is being tested!` I don't understand why `48` in the beginning. – Asaf Jan 08 '19 at 20:48
  • Well, that change makes no sense. You're now passing a string pointer where an integer parameter is expected, and an integer where a string pointer is expected. As for the "48" that gets printed, the low-order 16 bits of `s1`'s address probably happens to be `0x0030` (i.e. 48). But again, the change makes no sense, so you shouldn't read too much into the result. – Michael Jan 09 '19 at 06:59