-4

I missed the class on this topic because I am out of the country and I haven't been able to get the notes. Could you please lead me in the correct direction with this problem? It has been a while since I used MIPS so it's basically a foreign language to me :D

Homework question :

The MIPS architecture supports byte and halfword (16-bit) memory transfer operations. The instructions are load byte (lb), load byte unsigned (lbu), store byte (sb), load halfword (lh), load halfword unsigned (lhu) and store halfword (sh).

Code: char a, b; // 8-bit variables (a @ address 100) (b @ address 200)

Part a) Assuming 8-bit operations are supported (lb, lbu, sb), write a code that swaps the variables.

Part b) If MIPS doesn’t support byte and halfword operations, then we can only use ‘load word’ (lw) and ‘store word’ (sw), which are 32-bit operations. Accordingly, rewrite the code above using only (lw, sw) as memory transfer instructions.

Note : I am really not looking for you to solve it for me, I just don't know what to do. Do I write a swap function in C then convert to MIPS or am I making it overly complicated? Thank you!

CarleyDiaz
  • 35
  • 5
  • 1
    I'm voting to close this question as off-topic because the op is asking us to do his homework. – Styphon May 26 '16 at 12:38
  • 1
    I vote to close your response because 1) I am a lady , 2) I CLEARLY stated I don't want someone to solve it for me. – CarleyDiaz May 26 '16 at 12:49
  • That's what your teach is for, ask them. – Styphon May 26 '16 at 12:50
  • I can't ask my teacher , as I also stated I am out of the country, so in Italy I can not call or see my professor, I have emailed him and he has not responded. – CarleyDiaz May 26 '16 at 12:52
  • I just don't get why you have to be so rude, just trying to learn, not asking for someone to do my homework. It's 10 questions, this is the only one I need help with. – CarleyDiaz May 26 '16 at 12:53
  • I will take your silence as defeat. – CarleyDiaz May 26 '16 at 12:59
  • No, I'm just not going to get into an argument with you. That's not what this place is for. I'm not trying to be rude, IMO this isn't an appropriate question for SO. If others also agree with me they will also vote to close. Otherwise they may well answer your question. – Styphon May 26 '16 at 13:00
  • What is innapropropriate about it , I am trying to learn, I have no options! Google isn't useful , I can't reach a teacher or a student in my class, what do I do then, just lose 10%? I just want an explanation of what it even entails to solve it. Where else could I post for help? – CarleyDiaz May 26 '16 at 13:03
  • Two things, 1. you haven't even tried. You clearly have an idea but instead of trying it out you come straight here. Give your idea a try, even if you do go a round about way if you get it working you've completed the task. When reviewing it your teacher can explain that to you. And 2. you haven't explained that you've tried contacting your teacher and fellow students, and that googling didn't help, that you couldn't find any relevant answers. SO requires you to at least try, and if you get stuck we will help you. But at least have a go yourself. If you do get stuck post about what you've done – Styphon May 26 '16 at 13:06
  • And if you do need to post, post the code you've written and explain that you've tried contacting others for help (teacher and classmates) and tried googling but came up with nothing. If you show you've made an effort people will be much more likely to help you. – Styphon May 26 '16 at 13:07
  • Hi, I don't know anything about MIPS, but from what I understand, this is a course abount MIPS, so I think that the answer to "Do I write a swap function in C" is: no... you write the swap using lb, lbu and sb, as asked... If you don't know what these functions mean, you should be able to find them on Google (hint, there is lots of websites with lists of MIPS instructions). Good luck. – Basile Perrenoud May 26 '16 at 13:46
  • @Styphon H/W questions appear all the time on SO [the OPs just don't say it--OP here was being honest] and most get answers with full code. It is rare to close vote solely on that basis--it's not even a menu option. Usually, comment/downvote is sufficient. Close is usually for a post that is unclear and doesn't pose a specific question. Here, things are clear with specific question: "Do I write C code first and then convert to asm?". I've provided an answer below. Notice, I didn't write any mips code [OP said _not_ to], just the approach generally and OP still has [plenty of] work to do. – Craig Estey May 26 '16 at 18:51

1 Answers1

2

Writing C code first and using it as pseudo code for the mips assembly is a fine way to do this.

Then, reduce the C code to what I call "simple C". No while, for, or complicated expressions. No local variables. Only globals that are either things you define with .word like myvar: .word 23 or variables that correspond to the mips registers. Only very simple if (expr) goto label;

See my answer here for an example: Struggling with the logic in if statement

See my answer here: MIPS linked list This also has a bunch of tips about how to write clean asm code and an example of how to use C/pseudo code to construct asm code.

So, for part a, we're talking about unsigned char. Once the values have been fetched into register variables, apply a standard swap that you would do in C.

For part b, note that the addresses are aligned to 4 byte boundaries, so you can fetch/store them with 32 bit access. So, we're talking unsigned int. The wrinkle is that when the swap is done, proper masking/shifting/splitting has to be done to swap the bytes but not swap the remainder of the 32 bit words, using only unsigned int

So, write the C code first. You can fake the 100/200 with:

typedef union {
    unsigned int ival;
    unsigned char bval[4];
} membuf_t;

membuf_t adr100;
membuf_t adr200;

This is just to allow you to write real C code and test it first. For the asm, you can just reference the [hardwired] addresses (e.g. 0x100($zero)).

But, the 100/200 assumes you're just going to do this as a thought experiment and not try to actually debug your asm code using a simulator like mars or spim [because they really don't like you use these memory addresses in code they simulate].

If it were me, I'd use mars and write code that can be single stepped. For that, once again, adjust slightly:

adr100:     .word   0x01020304
adr200:     .word   0x05060708

Then, after everything is working, just replace the adr* references by hand.


UPDATE:

One thing I forgot to mention is that you have to be aware of endian issues. That is, for the C code when you do a 32 bit fetch from adrXXX.ival is the adrXXX.bval[0] in bits 0-7 (little endian) or bits 24-31 (big endian)?

Assuming you do the C code on an x86 PC, it's little endian. IIRC, mips is little endian as well, but, honestly, I forget [and some models can be big endian or can switch dynamically]. The simulators are [probably] little endian, so, maybe, no worries.

One way to test on mips (or C as well) is to use an extra test cell (i.e. create a 3-4 line throwaway test program): test: .word 0. Get a register filled with 0x00000001. Then, do a byte store to test. Then do a 32 bit load from test. The non-zero byte within the register gives you the endian for the machine (i.e. it's either in bits 0-7 or bits 24-31).

Community
  • 1
  • 1
Craig Estey
  • 30,627
  • 4
  • 24
  • 48