[Top] [All Lists]

Re: MIPS/Linux assembly issue

Subject: Re: MIPS/Linux assembly issue
From: adnan iqbal <>
Date: Tue, 25 May 2010 10:33:34 +0500
In-reply-to: <1274445001.9403.5.camel@localhost>
Original-recipient: rfc822;
References: <> <1274445001.9403.5.camel@localhost>
Dear Wu and Arnaud

Many many thanx for your help. Because of your help, i have been able to do the following.

1. Write an assembly program for octeon which starts with a routine __start.
2. Extend this program such that it calls a function written in C which resides in another file compiled separately but linked statically to make one executable.
Essentialy i use following sequence of commands for compilation and linking

$as hello2.s -o hello2.o

$gcc -c c_func.c -o c_func.o

$gcc -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -Wno-long-long -Wno-pointer-sign -Wdeclaration-after-statement -fno-stack-protector -static -Wl,-defsym,valt_load_address=0x40000000 -nodefaultlibs -nostartfiles -u start -Wl,-T,  -o hello2 hello2.o c_func.o -lgcc

All this is great but i need to do a bit more. I have to write a c program which has entry point  __start written in inline-assembly and makes use of c functions (and global data ) defined in same file. I wrote a program for this purpose and tried to compile and link it using different methods but it did not work. The program compiles but gives a segmentation fault. Below is the code for the program.

The code of hello_in_c.c
int f1()
return 0;

".globl __start\n"
".ent __start \n"
"__start: \n"
   "\t.set noreorder \n"
   "\t.cpload $gp\n"
   "\t.set reorder\n"
   "\tli $4, 1\n"
   "\tla $5, stradr \n"
   "\tlw $6, strlen\n"
   "\tli $2, 4004\n"
   "\tjal f1\n"
   "\tmove $4, $0\n"
   "\tli $2, 4001\n"
".end __start\n"
"stradr: .asciiz  \"hello, world!\\n\"\n"
"strlen: .word . - stradr"
thank you for your previous help and hope to get more from you.


On Fri, May 21, 2010 at 5:30 PM, Wu Zhangjin <> wrote:

Just found Arnaud have explained the problems, so here give you an
example I have written one year ago:

# File: hello.s -- Say Hello to MIPS Assembly Language Programmer
# Author: falcon <>, 2009/01/17
# Ref:
#    [*]
#    [*] MIPS Assembly Language Programmer's Guide
#    [*] See MIPS Run Linux(second version)
# Compile:
#       $ gcc -o hello hello.s
#       or
#       $ as -o hello.o hello.s
#       $ ld -e main -o hello hello.o

   .globl main

   .set noreorder
   .cpload $gp       # setup the pointer to global data
   .set reorder
                     # print sth. via sys_write
   li $a0, 1         # print to standard ouput
   la $a1, stradr    # set the string address
   lw $a2, strlen    # set the string length
   li $v0, 4004      # index of sys_write:
                     # __NR_write in /usr/include/asm/unistd.h
   syscall           # causes a system call trap.

                     # exit via sys_exit
   move $a0, $0      # exit status as 0
   li $v0, 4001      # index of sys_exit
                     # __NR_exit in /usr/include/asm/unistd.h

stradr: .asciiz "hello, world!\n"
strlen: .word . - stradr  # current address - the string address
# end

       Wu Zhangjin

On Fri, 2010-05-21 at 16:46 +0500, adnan iqbal wrote:
> Hi all,
> I am trying to compile/link/execute following very simple program in
> debian/MIPS (Tried on Qemu and Octeon). I am getting errors while
> executing the program. gdb also shows a strange behavior showing
> program entrypoint somehere in data segement. Any help getting this
> sorted out shall be appreciated.
> Regards
> Adnan
> Commands used to compile/link
> ----------------------------------------------------
> $ as hello.s -o hello.o
> $ld hello.o -o hello
> $ ./hello
> The code
> ---------------
>       .data
> str:
>         .asciiz "hello world\n"
>         .text
>         .globl __start
> __start:
>         jal f2
>         la $4,str
>         li $2,4
>         syscall
>         ## terminate program via _exit () system call
>         li $2, 10
>         syscall
> f2:
>         add $8,$8,$0
>         jr $31

<Prev in Thread] Current Thread [Next in Thread>