所以我试图交叉编译用汇编(使用 MARS(编写的简单程序以 mips 可执行文件。
# Purpose: First program, Hello World
.text # Define the program instructions.
main: # Label to define the main program.
li $v0,4 #Load 4into $v0 to indicate a print string.
la $a0, greeting #Load the address of the greeting into $a0.
syscall #Print greeting. The print is indicated by
# $v0 having a value of 4, and the string to
# print is stored at the address in $a0.
li $v0,10 #Load a 10 (halt) into $v0.
syscall # The program ends.
.data # Define the program data.
greeting: .asciiz "Hello World" #The string to print.
首先我使用
mips-linux-as ./HelloWorld.asm -o HelloWorld.o
然后
mips-linux-ld HelloWorld.o -o HelloWorld
我收到一个错误
mips-linux-xld: warning: cannot find entry symbol __start; defaulting to 00000000004000b0
简单地添加_start部分不起作用,我得到同样的错误。所以我用C(HelloWorld.c(编写了简单的程序,并使用以下命令使用gcc生成程序集
mips-linux-gcc -O2 -S -c HelloWorld.c
并且没有_start部分
.file 1 "HelloWorld.c"
.section .mdebug.abi32
.previous
.gnu_attribute 4, 3
.abicalls
.section .rodata.str1.4,"aMS",@progbits,1
.align 2
$LC0:
.ascii "Hello Worldn 00"
.text
.align 2
.globl main
.set nomips16
.ent main
.type main, @function
main:
.frame $sp,32,$31 # vars= 0, regs= 1/0, args= 16, gp= 8
.mask 0x80000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-32
sw $31,28($sp)
lui $28,%hi(__gnu_local_gp)
addiu $28,$28,%lo(__gnu_local_gp)
.cprestore 16
lui $4,%hi($LC0)
lw $25,%call16(printf)($28)
jalr $25
addiu $4,$4,%lo($LC0)
move $2,$0
lw $31,28($sp)
j $31
addiu $sp,$sp,32
.set macro
.set reorder
.end main
.size main, .-main
.ident "GCC: (GNU) 4.4.5-1.5.5p4"
那么我错过了什么?我想在我的 mips 盒子上运行这个程序。
以下是 Linux 上 MIPS 的 hello-world 可执行文件(不是 MARS(:
# file hello.s
.data
msg: .ascii "Hello, World!n"
len = . - msg
.text
.globl __start
__start:
li $v0, 4004 # __NR_write == 4004
li $a0, 1 # argument: STDOUT_FILENO
la $a1, msg # argument: message to write
li $a2, len # argument: size of message
syscall
li $v0, 4001 # __NR_exit == 4001
li $a0, 0 # argument: EXIT_SUCCESS
syscall
在 Debian 和 Ubuntu Linux 上,安装汇编程序和链接器(用于编译(,如下所示:
$ sudo apt-get install binutils-mips-linux-gnu
在 Debian 和 Ubuntu Linux 上,像这样安装模拟器:
$ sudo apt-get install qemu-user
编译成大端可执行文件,如下所示:
$ mips-linux-gnu-as -EB -mips1 -o hello.o hello.s
$ mips-linux-gnu-ld -s -m elf32btsmip -o hello hello.o
在模拟器中运行大端可执行文件,如下所示:
$ qemu-mips ./hello; echo $?
Hello, World!
0
编译成小端序可执行文件,如下所示:
$ mips-linux-gnu-as -EL -mips1 -o hellol.o hello.s
$ mips-linux-gnu-ld -s -m elf32ltsmip -o hellol hellol.o
在模拟器中运行小端序可执行文件,如下所示:
$ qemu-mipsel ./hellol; echo $?
Hello, World!
0
尝试将其添加到您的 asm 文件中
.globl main
.ent main
.type main, @function
但是,您将需要使用 MIPS Linux 系统调用,而不是 MARS 系统调用。