织梦CMS - 轻松建站从此开始!

罗索

MIPS Assembly Programming Guide

落鹤生 发布于 2010-04-01 22:08 点击:次 
The MIPS is a RISC microprocessor. It is small, powerful, and standard on many embedded system and advanced computing systems (Corporations such as SGI, NEC and MIPS technology still produced them in large quantities).
TAG:

The MIPS is a RISC microprocessor. It is small, powerful, and standard on many embedded system and advanced computing systems (Corporations such as SGI, NEC and MIPS technology still produced them in large quantities). The MIPS assembly often frustrates new users with its unfamiliar sequence of operation. Hereby I provide a startup kit for the newbies to quickly master the art of programming MIPS processors in assembly.

Entering the MIPS World


First, we start with a simple assembly file and show how it could be compiled in the R2000 format in a R10000 plus machine equipped with IRIX operating system. Users who doesn't have a MIPS machine at hand can use the freely available SPIM (with X Window and Windows support) to simulate MIPS R2000.

.ent main
.globl main
main:
li $3, 1
li $2, 2
addu $4, $2, $3
.end main

We named this file as "test.s" which does do anything other then adding two general register ($2, $3) together and store the result into another register ($4).

Other than that, there are two menomics for assembly language including the ".ent" and ".end" command denoting the begining and end of an function object (entity).

After that, we can assembly it and link it into an executable ELF file.

Here is the command sequence:

as -mips1 test.s -n32 -o test_4.o

ld -L/usr/lib32/mips4 -L/usr/lib32 /usr/lib32/mips4/crt1.o test_4.o -lc /usr/lib32/mips4/crtn.o

Some brief explanation: "as" is the standalone assembler provided by the SGI MIPS C compiler suite. We use "-mips1" to generate the R2000 compatible code which are easier to analyze. R2000 is the standard RISC processor covered in computer architecture textbook. Then -n32 generate a (new) 32-bit object which are required for the (new) linker editor we used. This will produce a warning since (new) object are usually used together with "-mips3" or "-mips4".
Then we use the linker editor "ld" provided. We linked the object file with the mips4 compatible library since most machines doesn't have older library.
"crt1.o" and "crtn.o" are the so called 'C-startups' files belonging to the C library. meanwhile the 'C++-startups': 'crtbegin.o' and 'crtend.o' belong to GCC. Sometimes the C-library has not the 'crti.o' and 'crtn.o', maybe not even the 'crt1.o'. This is the case with Solaris2. MIPS elf use both of them.

Then you can play around with these compiled executable file and object file with the following useful command.

nm test_4.o

It prints out the symble table of this object file.

Also you can disassemble this object file by

dis test_4.o


How to build a MIPS crosscompiler using GNU toolchain (gcc)?

Here I show how to build a cross tool chain for a little endian mips-linux target on x86-linux host. The procedure is basically borrowed from Wei Qin's description on how to build a GNU toolchain on ARM .

Decide where are you going to put in your source, build directory and the installlation directory.
Create one SRC_DIR and place all above packages under SRC_DIR.
Create one PREFIX as where you want the generated cross tool chain to be.
Create one BUILD_DIR as your working folder.
Set environmental variables SRC_DIR, PREFIX, BUILD_DIR to their corresponding directory, use ABSOLUTE path.
Most time, the configure scripts are decide its own host type.
For example: PREFIX=/u/xzhu/mips-gcc
SRC_DIR=/u/xzhu/crossgccsrc
TARGET=mips-unknown-linux

Step 1: Build the binutils

cd $SRC_DIR
wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.11.2.tar.gz
tar xzvf binutils-2.11.2.tar.gz
cd $BUILD_DIR
mkdir binutils
cd binutils
$SRC_DIR/binutils-2.11.2/configure --prefix=$PREFIX --target=$TARGET
make
make install

Add $PREFIX/bin to your current PATH. In bash, this is
   export PATH=$PREFIX/bin:$PATH

Step 2 -- Build gcc

SUBSTEP1 -- Configure linux kernel to get header files (we use the CVS server of MIPS-linux)

cd $SRC_DIR
cvs -d :pserver:cvs@ftp.linux-mips.org:/home/cvs login
(Only needed the first time you use anonymous CVS, the password is "cvs")
cvs -d :pserver:cvs@ftp.linux-mips.org:/home/cvs co linux
make menuconfig (Do nothing but save configuration and exit)
make dep
mkdir $PREFIX/$TARGET/include
cp -dR $SRC_DIR/linux/include/asm-mips $PREFIX/mips-unknow-linux/include/asm

SUBSTEP2 -- Build gcc

cd $SRC_DIR
wget ftp://gcc.gnu.org/pub/gcc/releases/gcc-2.95.3/gcc-2.95.3.tar.gz
tar xzvf gcc-2.95.3.tar.gz

cd $BUILD_DIR
mkdir gcc
cd gcc
$SRC_DIR/gcc-2.95.3/configure --target=$TARGET --prefix=$PREFIX --with-headers=$SRC_DIR/linux/include --disable-threads --enable-languages=c

Edit the file "tconfig.h" in "$BUILD_DIR/gcc"
Add the following lines at the end
#ifndef inhibit_libc
#define inhibit_libc
#endif
so it won't use the glibc which hasn't been built yet

cd $BUILD_DIR
make

Ignore the error message saying that the built C compiler cannot produce executables.

make install

SUBSTEP3 -- Unpack glibc

cd $SRC_DIR
wget http://mirrors.theonlinerecordstore.com/gnu/glibc/glibc-2.2.3.tar.gz
wget http://mirrors.theonlinerecordstore.com/gnu/glibc/glibc-linuxthreads -2.2.3.tar.gz
tar xzvf glibc-2.2.3. tar.gz
cd glibc-2.2.3
tar xzvf ../glibc-linuxthreads-2.2.3.tar.gz


STEP4 -- Build glibc

Set environmental variable CC as mips-unknow-linux-gcc, in bash
   export CC=mips-unknow-linux-gcc

Clear LD_LIBRARY_PATH, in bash, this is
   export LD_LIBRARY_PATH=

cd $BUILD_DIR
mkdir glibc
cd glibc
../gcc-2.95.3/configure -prefix=/u/xzhu/crossgcc-mips -target=mips-unknow-linux --with-headers=../linux/linux-mips/include -enable-languages=c

make
cd ..
rm glibc/libc.so.6 (This avoids linux takes the cross libc.so.6 as its own dynamic library and screws commands like make or ls.)
cd glibc
make install (The deleted libc.so.6 will be recreated)

Reference on MIPS

A good tutorial from a class .

A Quick Start on MIPS Assembly .

MIPS Programming Book containing a lot of examples.

Class Notes on MIPS programming Very good .

Programmer's Guide (pdf) .

Some Books as well. I recommend the classic: MIPS RISC Architecture and a newer book See MIPS Run .

Last modified: 29 March 2003
(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201004/8968.html]
本文出处:chortle.ccsu.edu 作者:秩名
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容