2

I'm wondering if there is a way to write MIPS assembly language file (just really simple test file) with a .s extension like is used with the PCSpim simulator and then have it actually compile the code and produce a file with the hex output as if I were to load it onto an actual MIPS chip.

Can SPIM or some other similar software do this? Can you explain a little more about the assembly process from MIPS assembly to the actual executable that the microcontroller uses?

Thanks


EDIT: So I'm running on ubuntu linux with gcc installed. I have a mips file mipsTest.s that is the following

 .text
 add $7, $0, $0

Now at the command line I type

gcc -c -o mipsy.o mipsTest.s

And the error that I get says

mipsTest.s: Assembler messages:
mipsTest.s:2: Error: at most 2 immediate operands are allowed.

So to me that means that the assembler is not reading the mips language, it is probably assuming my assembly file is in x86? How do I get a cross compiler setup so that gcc will recognize the MIPS assembly language instead of x86? (or whatever its interpreting my mipsTest.s file as right now) Thanks!

EDIT 2: So I googled "mips cross-assembler" and found a few things. The one that seemed most promising was the umps cross-assembler project on sourceforge. So I downloaded umps2.0 which is supposed to be a stable version. I untarballed it any ran the "./configure" as instructed in the readme file. Then my console gave me the following error which prevents me from running the make command...

jonathan@mini-heiretic:~/Documents/cross-compiler/umps-2.0$ ./configure
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking whether byte ordering is bigendian... no
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking how to run the C preprocessor... gcc -E
checking whether ln -s works... yes
checking how to print strings... printf
checking for a sed that does not truncate output... /bin/sed
checking for fgrep... /bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert i686-pc-linux-gnu file names to i686-pc-linux-gnu format... func_convert_file_noop
checking how to convert i686-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for mt... mt
checking if mt is a manifest tool... no
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking dependency style of gcc... gcc3
checking for ld used by gcc... (cached) /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... (cached) yes
checking for elf_version in -lelf... no
configure: error: *** Libelf not found. Specify a different path or install it. --help for info.

Are there any better options I should try or can anyone provide input on my error?

EDIT 3: After downloading Libelf, I can now get a little further in the configure script for umps. Here is the output starting from the newly successful elf_version line

checking for elf_version in -lelf... yes
checking for dlopen in -ldl... yes
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for SIGCPP... no
configure: error: Package requirements (sigc++-2.0) were not met:

No package 'sigc++-2.0' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables SIGCPP_CFLAGS
and SIGCPP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

So I ran a quick sudo apt-get install sigc++-2.0 but couldn't find the package... suggestions?

EDIT 4: Here's the error I get when I try to use the Makefile

jonathan@mini-heiretic:~/Documents/cross-compiler/dwelch67-pic32_samples-f34fb22/blinker01$ make
mips-sde-elf-as -EL -G0 --warn --fatal-warnings blinker01.s -o blinker01.o
make: mips-sde-elf-as: Command not found
make: *** [blinker01.o] Error 127
NickHalden
  • 1,469
  • 2
  • 20
  • 31
  • https://github.com/dwelch67/pic32_samples I have some examples and instructions on how to build your own cross compiler/tools as well as ones you can just download. – old_timer Mar 03 '12 at 01:56
  • Thanks dwelch, so which files do I need to download for the MIPS cross-assembler? It looks from your readme that all I need is the binutils package if I plan to use gcc as the front-end for the mips .s files? – NickHalden Mar 03 '12 at 02:16
  • binutils is all you need. Now spim may have syntax specific to spim that the gnu assembler may or may not support so you might have to massage the code to get from where you are now to something that gas likes. see the blinker01 example as a simple assembly to machine code example. – old_timer Mar 03 '12 at 03:29
  • @dwelch ok thanks i'll try that out in a minute. Yeah SPIM supports some pseudoinstructions that are just implemented as macros, but I don't want the assembler to deal with those. For my purposes I only want the actual MIPS 1 instructions. – NickHalden Mar 04 '12 at 21:12
  • @dwelch ok so I downloaded binutils and untar'd it. I then did a configure make and make install. What do I need to do now so that gcc can recognize the mips instructions? – NickHalden Mar 04 '12 at 22:34
  • gcc does not recognize mips instructions it creates mips instructions or it calls the assembler (okay maybe it recognizes inline assembly). you use the binutils tools gnu assembler or you feed gcc the .s file and it will take care of it for you. basically look at the examples in that project, the blinker01 example. there is mips assembly code and a make file that uses the gnu tools to assemble and link into a binary. – old_timer Mar 05 '12 at 00:25
  • Ok thanks, starting to understand it. If you wrote up a good explanation of how the makefile works as an answer I'd probably mark that as the correct one soon as I get this working (even though Carl's was good too, I just gave him an upvote). – NickHalden Mar 05 '12 at 02:21
  • @dwelch so I'm trying to make the blinker01 example and I get an error indicating that I didn't quite install the GNU tools correctly. See edit4 in the question... thanks. – NickHalden Mar 05 '12 at 03:18
  • did you set your path to the tools? PATH=/wherever/bin:$PATH where wherever is what you put on the --prefix=/wherever add the /bin to that. Or use the full path /wherever/bin/mips-sde-elf-as --version. – old_timer Mar 05 '12 at 15:09
  • @dwelch ok great, so I got your blinker01 example to compile/make and I'm looking at the resultant blinker01.hex file, the blinker01.hex.hex file, and the blinker01.list file. Now what I need is just a copy of what would be loaded into the chips memory, like all the instructions in hex that I can see on the left side of the list file. Is that what these hex files represent? Also, why do you skip certain lines from the .hex file when you make the .hex.hex file? Thanks so much for your help, real close to getting what I need... – NickHalden Mar 05 '12 at 22:32
  • the tool that I used to load the file had a problem with the .hex files being created by the gnu tools, if I remember right, so I stripped out the offending parts. you may not have that issue. – old_timer Mar 05 '12 at 23:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8556/discussion-between-jgord-and-dwelch) – NickHalden Mar 05 '12 at 23:45

1 Answers1

3

You can use gcc, among others, to cross-compile or assemble MIPS assembly language source. As far as the process, there is a one-to-one mapping between assembly language mnemonics and machine instructions. The assembler just does the translation for you.

Edit: You're not using a cross-assembler, you're using the system's own gcc. That toolchain is presumably targeting Intel processors, which don't have a 3-operand add instruction. You need to use a cross-assembler for MIPS. Download and install a binary one, or build a toolchain yourself. Then you'll have a cross compiler (maybe called mips-gcc or something like that) that you can use to assemble your MIPS source files.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Thanks, mind expanding a little bit on how to use gcc for the cross-compilation process? Kind of a noob with the whole C environment stuff.... – NickHalden Feb 23 '12 at 00:27
  • Well, go get a MIPS toolchain (or build one yourself), and then use `gcc -o myApp sourceFile.s" and that should be about it. You might need to do some more complicated things depending on what the environment on your chip is like. – Carl Norum Feb 23 '12 at 01:00
  • I do not want my chip to be running a kernel or anything like that. It should simply be a state machine executing the hex instructions which are the result of the one-to-one mapping from sourceFile.s which you described in your answer. Surely there exists a program which can do the one to one translation for me? I'm not looking to build a C library or anything. Just go straight from MIPS assembly to MIPS machine code. – NickHalden Feb 23 '12 at 04:00
  • The assembler will do that. You can use `objcopy` to get the flat binary out of the object file after you've assembled the source. – Carl Norum Feb 23 '12 at 15:48
  • Would that be the GNU assembler? I'm sorry but the keywords you are giving me are not turning up any directly useful websites. I want to mark your answer correct because I'm sure you know what you're talking about, but unless you can provide either a list of steps or a more directly helpful link I will have to rework the question and ask it again. Please elaborate on exactly the steps I need to take for taking a .s file and retrieving the object file that is produced by any MIPS assembler. Thanks – NickHalden Feb 26 '12 at 05:31
  • Sure if you want. Normally it's easier to invoke the assembler through the `gcc` front end. `gcc -c -o example.o example.s` should do it. You'll need to replace `gcc` with the name of your cross-compiler front end. – Carl Norum Feb 26 '12 at 05:34
  • Sorry, made another edit. This cross-assembler thing is giving me a lot of trouble. – NickHalden Mar 03 '12 at 00:37
  • It says you don't have libelf and that you should get it and install it - is that not possible for some reason? – Carl Norum Mar 03 '12 at 03:52
  • Lots of good information here: http://stackoverflow.com/questions/4175450/is-there-a-way-to-use-gcc-to-convert-c-to-mips – Carl Norum Mar 03 '12 at 03:55
  • Downloaded and installed Libelf, now there's a no problem... see Edit3. Also, that other question is mainly about compiling MIPS from C code... not really what I'm trying to do – NickHalden Mar 04 '12 at 23:34
  • If you have a C toolchain, it will include an assembler. – Carl Norum Mar 04 '12 at 23:35