-1

Okay, so the question here is "What does $v0 have at end of the loop execution?", I found out the answer 3628800 from a friend, and he wrote some python code that translated the Mips, essentially the code just means 10! (factorial of 10, not me yelling 10 lol)

heres the python code if you'd like that too:

a = 10
v = 1
while (a != 0):
    v *= a
    a -= 1
print (v)

Now, I understand what the python code does, and I understand that it translates into the factorial of 10, but in the mips assembly, I thought I understood the logic of it, actually, I made a crappy ms paint diagram here where I thought it was either 9, 10, or 11 (I know, not very specific, but I'm still rather new to this :( please don't judge me) Heres the picture if you'd like that too :)


.text
.globl main

main:
li $a0, 10


li $v0, 1
Loop:
beq $a0, $zero, exit
mul $v0, $a0, $v0
add $a0, $a0, -1
jal Loop
exit:
li $v0, 10
syscall

Now, you're probably wondering why I'm asking a question even though I've seemingly solved the problem either way, plus added my own thinking to why I had previously believed it was 10, (well, in the picture I drew I said 9 or 11, but I also meant 10 too lol) and the answer to that would be that I just don't understand the logic behind why Mips does what it does and instead of just printing out 10, (although the jal instruction is probably where that lies and does the factorial stuff), anyways, any help here would be appreciated a lot! :) Thank you S.O.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    So... What do you think the `jal` instruction does? And what do you think is going to happen to `v0` after it does it? And perhaps more importantly, once the loop is complete, what will `li $v0, 10` do to the value you've been computing in `v0`? – David Wohlferd Sep 08 '17 at 05:01
  • Well, if I understood the resources I read correctly, the `jal` instruction should enable me to call functions, e.g. Loop, from what im reading, it copies the address of the next instruction into register 31 and then it should jump to the address Loop, right? :? And what I think will happen to v0 is that currently its set to an integer of 1, (before it goes through the Loop function), but then it reaches the `beq` instruction, and I get kind of fuzzy there, the documentation says that it branches if two registers are equal, but it doesent mention what happens when the offset ($v0) is equal to- – Rudy Peralta Sep 08 '17 at 05:18
  • - itself, ugh, I feel kind of dumb for not understanding `beq` :/... Actually, without knowing that I can't even give you a correct answer... – Rudy Peralta Sep 08 '17 at 05:19
  • 1
    Is this one of the MARS/SPIM platforms? Then use the debugger and single-step the instructions, it should be easier to understand what the CPU actually does. The `jal` is not a good solution in this case, while it will keep jumping to your loop, it will also destroy content of `r31`, which is often needed to "return" from your current function (not needed for you, as you exit by calling `syscall, v0=10`). Your asm source contain no instruction involved in printing the value. You can print integer in MARS/SPIM by calling `syscall` with `v0=1, a0=`, check the MARS syscall docs. – Ped7g Sep 08 '17 at 09:08
  • https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html – Ped7g Sep 08 '17 at 09:09
  • And the `beq` does branch when two registers are equal. That's all. Nothing else does change/happens/affect that, only the two registers mentioned in the `beq` line, and when they equal, the next instruction executed is at the target branch address... (that said, if I recall it correctly, a real MIPS CPU has delayed branch execution, so even when `a0 == 0`, and the flow control will go to `exit`, it will still execute also the `mul` ... but this real-world feature tend to be switched off in MARS/SPIM emulators for students, as it's quite confusing at the beginning). – Ped7g Sep 08 '17 at 09:14
  • why do you use `jal` when there's no function call in the loop? – phuclv Aug 13 '18 at 05:13
  • see [Necessity of J vs. JAL (and JR vs. JALR) in MIPS assembly](https://stackoverflow.com/q/41175942/995714) – phuclv Aug 14 '18 at 16:42

1 Answers1

1

The explanation here is easy.

During the loop what happens is that the value of $a0 goes down one by one, and the execution stays as below:

1st - $a0 = 10 and $v0 = 1 -> In the `mult` operation (multiply) we have: $v0 = 10 * 1 ($a0 * $v0) -> 10
2nd - $a0 = 9 and $v0 = 10 -> In the `mult` operation (multiply) we have: $v0 = 9 * 10 ($a0 * $v0) -> 90
3rd - $a0 = 8 and $v0 = 90 -> In the `mult` operation (multiply) we have: $v0 = 8 * 90 ($a0 * $v0) -> 720
...
8th - $a0 = 3 and $v0 = 604800 -> In the `mult` operation (multiply) we have: $ v0 = 3 * 604800 ($a0 * $v0) -> 1814400
9th - $a0 = 2 and $v0 = 1814400 -> In the `mult` operation (multiply) we have:  $v0 = 2 * 1814400 ($a0 * $v0) -> 3628800
10th - $a0 = 1 and $v0 = 3628800 -> In the `mult` operation (multiply) we have: $v0 = 1 * 3628800 ($a0 * $v0) -> 3628800

Then the value of $a0 becomes 0, and then the execution of exit occurs.

The detail is in the question, because it wants to know the final value of $v0 at the end of the loop, that is, $v0 = 3628800

But the program would still continue, rewriting the value from $v0 to 10, but that's the catch, the answer is to the end of the loop, that end does not come in.

Djpremier
  • 26
  • 2