All of lore.kernel.org
 help / color / mirror / Atom feed
* MIPS/Linux assembly issue
@ 2010-05-21 11:46 adnan iqbal
  2010-05-21 12:21 ` Arnaud Patard
  2010-05-21 12:30 ` Wu Zhangjin
  0 siblings, 2 replies; 4+ messages in thread
From: adnan iqbal @ 2010-05-21 11:46 UTC (permalink / raw)
  To: linux-mips

[-- Attachment #1: Type: text/plain, Size: 781 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 910 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: MIPS/Linux assembly issue
  2010-05-21 11:46 MIPS/Linux assembly issue adnan iqbal
@ 2010-05-21 12:21 ` Arnaud Patard
  2010-05-21 12:30 ` Wu Zhangjin
  1 sibling, 0 replies; 4+ messages in thread
From: Arnaud Patard @ 2010-05-21 12:21 UTC (permalink / raw)
  To: adnan iqbal; +Cc: linux-mips

adnan iqbal <adnan.iqbal@seecs.edu.pk> writes:

> Hi all,

Hi,
>
> 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

Please consider adding ".ent __start" here

>
> __start:
>         jal f2
>         la $4,str
>         li $2,4

It's wrong here. On o32, write syscall is 4004. Also the args are:
$4: file descriptor (here 0)
$5: string
$6: string length

>         syscall
>
>         ## terminate program via _exit () system call
>         li $2, 10

exit is 4001. 4010 is unlink. Look at /usr/include/asm/unistd.h on your
mips system.

>         syscall

Add .end __start here if you've added the .ent


Arnaud

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: MIPS/Linux assembly issue
  2010-05-21 11:46 MIPS/Linux assembly issue adnan iqbal
  2010-05-21 12:21 ` Arnaud Patard
@ 2010-05-21 12:30 ` Wu Zhangjin
  2010-05-25  5:33   ` adnan iqbal
  1 sibling, 1 reply; 4+ messages in thread
From: Wu Zhangjin @ 2010-05-21 12:30 UTC (permalink / raw)
  To: adnan iqbal; +Cc: linux-mips

Hi,

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 <wuzhangjin@gmail.com>, 2009/01/17
# Ref: 
#    [*] http://www.tldp.org/HOWTO/Assembly-HOWTO/mips.html
#    [*] 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

    .text
    .globl main
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
    syscall

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

Regards,
	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
>      

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: MIPS/Linux assembly issue
  2010-05-21 12:30 ` Wu Zhangjin
@ 2010-05-25  5:33   ` adnan iqbal
  0 siblings, 0 replies; 4+ messages in thread
From: adnan iqbal @ 2010-05-25  5:33 UTC (permalink / raw)
  To: wuzhangjin; +Cc: linux-mips

[-- Attachment #1: Type: text/plain, Size: 4420 bytes --]

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,valt_load_address_mips64_linux.lds  -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()
{
//called
return 0;
}

asm(
".text\n"
".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"
   "\tsyscall\n"
   "\tjal f1\n"
   "\tmove $4, $0\n"
   "\tli $2, 4001\n"
   "\tsyscall\n"
".end __start\n"
"\t.rdata\n"
"stradr: .asciiz  \"hello, world!\\n\"\n"
"strlen: .word . - stradr"
);
-----------------------------------------------------------------
thank you for your previous help and hope to get more from you.

Regards
Adnan





On Fri, May 21, 2010 at 5:30 PM, Wu Zhangjin <wuzhangjin@gmail.com> wrote:

> Hi,
>
> 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 <wuzhangjin@gmail.com>, 2009/01/17
> # Ref:
> #    [*] http://www.tldp.org/HOWTO/Assembly-HOWTO/mips.html
> #    [*] 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
>
>    .text
>    .globl main
> 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
>    syscall
>
>    .rdata
> stradr: .asciiz "hello, world!\n"
> strlen: .word . - stradr  # current address - the string address
> # end
>
> Regards,
>         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
> >
>
>
>

[-- Attachment #2: Type: text/html, Size: 5700 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-05-25  5:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-21 11:46 MIPS/Linux assembly issue adnan iqbal
2010-05-21 12:21 ` Arnaud Patard
2010-05-21 12:30 ` Wu Zhangjin
2010-05-25  5:33   ` adnan iqbal

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.