Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a function in MIPS with global variables?

frogger:
    jal teechar
    jal update_frog
    lw $t0,game_indicator
    li $t0, 0

Okay, this is the function. I have teechar and update_frog functions completely written and working. I understand the first two bullets but not sure on the last two. How would i write the last two bullets in MIPS?

HERE IS FUNCTION Instructions:

Write a MIPS function frogger that takes no arguments, and returns a character (to make testing easier). It does the following:

  • Call your teechar to read-and-echo a character c.
  • Call your update_frog(c).
  • If c is non-zero, store c in a global variable to indicate that the game is over, and how it ended. (You must initialize that global variable to zero, of course.)
  • Return c.

Here is my code:

frogger:
    jal teechar
    jal update_frog

Sorry I am just having trouble thinking about how to do this :(

like image 427
user977154 Avatar asked Feb 22 '26 03:02

user977154


1 Answers

For the sake of a global variable, either you have to pass the arguement or use a special purpose register. Since you have specified the function takes no args, you will have to reserve a register to hold the value of c whenever frogger is called, which would then read the value from that register.

Its not a clean solution from the high level programming language, but you are in assembly right now and might as well get your hands dirty!

like image 71
ConfusedAboutCPP Avatar answered Feb 27 '26 04:02

ConfusedAboutCPP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!