I want to ask, if Mips
assembly, have something similar to generic function, i understand that work with registers, and just need to load enother values, and go to procedure, but what if it's the same format of value?
I will try to explain myself with example and i hope you will understend what i mean.
I have some array and need to calculate the summary of elements in two ways, first in sign format, and after that in unsigned format. The comands must be different, like add
/addu
etc. inside the procedure. Can i use the same procedure for it? If it's posible, in what way?
I hope you will understend my language,
Thank's for help!
Asked
Active
Viewed 92 times
1

Michael
- 57,169
- 9
- 80
- 125

Alex Vartanov
- 71
- 1
- 6
-
MIPS uses 2's complement representation for signed integers, so the only difference between `addu` and `add` is that `add` raises an exception on signed overflow. Other than that, it's the exact same binary operation. You can use `addu` for signed or unsigned integers, and the MIPS manual even says so. – Peter Cordes Nov 03 '18 at 02:40
-
But it sounds like what you're looking for is an assemble macro, so you can define a big block of code once, and expand it twice with one thing being different. You'll have to check if the assembler built-in to MARS supports macros. BTW, if you want to use `add` for signed-overflow checking, I assume you're supposed to manually compare/branch for unsigned overflow checking after `addu` in the unsigned version? – Peter Cordes Nov 03 '18 at 02:42
-
Actualy if i load first member of my array with `lbu` and jump to next members, its do what i need, its convert numbers to unsigned (i have a few negative numbers) , but i afrade if all of variant of number will work, because i use regular `add` for summing and not `addu` – Alex Vartanov Nov 03 '18 at 02:49
-
Oh, you didn't say you had an array of bytes, I was assuming words. Yes, it matters whether you sign-extend (`lb`) or zero-extend (`lbu`) from byte to word when loading, but after that you should use `addu` in both, like a C compiler would. And if you need to do any comparisons, then interpretation matters. But for addition it doesn't. – Peter Cordes Nov 03 '18 at 03:03
-
Oh yes, i forgot it, sorry. Thank you for helping! – Alex Vartanov Nov 03 '18 at 17:28
-
1You can have some value somewhere to check, if the inner part of code should do this or that, but it all must be written in code, and usually it's better (for performance, but often also for source clarity) to have specialized variants of routines for every case, so there is no dynamic test in code by type in some inner loop, but somewhere outside. The CPU has pointer to next instruction to execute, it doesn't even know when it is in "procedure" or what is the "type" of value in register, it's only aware there are 32 0/1 values in register, the interpretation is left to the code. – Ped7g Nov 03 '18 at 22:20
-
That's on the CPU level. The assembler as language of course may introduce extra features, like for example MIPS assemblers do support many pseudo-instructions to make some tasks shorter to write, but generally assemblers try to stay close to machine code, not introducing too much extra abstraction, so by reading asm source you can often predict exactly what machine code will be produced, so the feature you are asking about is maybe doable in some assemblers trough some macro/preprocessor extras, but I can't recall anything which would perfectly fit your question(neither for MIPS or other CPU) – Ped7g Nov 03 '18 at 22:24