0

I want to compile assemble code (MIPS) by using C, while I came across the problem about "no register name". The error information is:

ABS.c:8:2: error: unknown register name ‘$s1’ in ‘asm’

and my code (ABS.c) are as follow:

#include<stdio.h>
#include<stdlib.h>

int ABS(int x)
{
int x_abs;

__asm__ __volatile__(
    "move $s1, %1\n"
    "bgez $s1, DONE\n"
    "NOP\n"
    "mult $s1, #-1\n"
    "mflo $s1\n"
    "DONE: move %0, $s1\n"
    : "=r"(x_abs)
    : "r"(x)
    : "$s1");
return x_abs;       
}

int main()
{
int result = ABS(2);
printf("%d", result);
system("pause");
return 0;
}

I wonder how I could fix that problem, THX!

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Xu Chengzhe
  • 383
  • 2
  • 3
  • 8
  • 5
    Are you *sure* you're compiling for MIPS? Run `gcc -v`. If you see x86-64 or i386, you're using a compiler that targets your x86 desktop. Your code compiles fine on MIPS gcc5.4 on Godbolt: https://godbolt.org/g/LQ9t35. And gives the same error you describe with x86 gcc. – Peter Cordes Apr 22 '18 at 08:17
  • 1
    Alternatively, if you have an old version of gcc/binutils: https://stackoverflow.com/questions/721192/is-it-possible-to-use-mips-register-names-with-gas-gnu-assembler – Matteo Italia Apr 22 '18 at 08:21
  • 1
    @Matteo: The OP's error message is from the compiler trying to process the clobber list; it hasn't made it to the assembler yet. The dup you linked will be a problem later, but gcc5.4 *does* understand `$s1`. Ah, I see you un-duped it :) – Peter Cordes Apr 22 '18 at 08:21
  • 1
    @PeterCordes: eh the problem with the dupe-hammer is that it's too direct; I merely wanted to suggest it, and thought I had no gold badge in [assembly] - but this is [C]! About the clobber list: that does explain the strange error message, indeed I expected the assembler to balk immediately on `move` if it was being processed by x86 as. – Matteo Italia Apr 22 '18 at 08:24
  • 1
    @MatteoItalia: Ah I see. For me it's sometimes the other way around; I mostly follow tags that tend to also have asm and/or x86 tags, so it's an unpleasant surprise when it turns out the OP only tagged x86-64 or whatever, and not assembly or C or C++, or something like that. At least once I've asked someone else to edit the tag list before I spend my close vote, when I notice ahead of time :P – Peter Cordes Apr 22 '18 at 08:58

1 Answers1

0

All registers in MIPS are accessible through symbolic names or numbers. I had the same issue, I solved it by using numbers instead of symbolic names, e.g. mov $17, %1 instead of mov $s1, %1

Bogi
  • 2,274
  • 5
  • 26
  • 34