[9fans] Hello Assembly - Plan9

This is a discussion on [9fans] Hello Assembly - Plan9 ; Hi, I'm trying to make a Hello World program in assembly without depending on libc. Here's what I have so far: DATA string+0(SB)/7, $"Hello\n\z" GLOBL string+0(SB), $7 TEXT _main+0(SB), 1, $0 // first arg; $1 = stdout MOVL $1, (SP) ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 29

Thread: [9fans] Hello Assembly

  1. [9fans] Hello Assembly

    Hi,

    I'm trying to make a Hello World program in assembly without depending
    on libc. Here's what I have so far:

    DATA string+0(SB)/7, $"Hello\n\z"
    GLOBL string+0(SB), $7

    TEXT _main+0(SB), 1, $0

    // first arg; $1 = stdout
    MOVL $1, (SP)
    // second arg, address of string
    MOVL $string+0(SB), 4(SP)
    // third arg, $7 = length of string
    MOVL $7, 8(SP)
    // fourth argument, -1LL (vlong offset)
    MOVL $-1, 12(SP)
    MOVL $-1, 16(SP)

    // use pwrite syscall
    MOVL $51, AX
    INT $64
    RET

    The program assembles and links fine - but executing it gives me:

    8.out 1183: suicide: invalid address 0x7 in sys call pc=0x104e

    It's probably something silly - I'll be extremely grateful if someone
    could point out what I'm doing wrong.

    Thanks!

    --
    Anant

  2. Re: [9fans] Hello Assembly

    > It's probably something silly - I'll be extremely grateful if someone

    there might be more, but you haven't allocated a stack frame for the (system) call


  3. Re: [9fans] Hello Assembly

    Hi Charles,

    > there might be more, but you haven't allocated a stack frame for the
    > (system) call


    I did think that was the case earlier, so I tried:

    TEXT _main+0(SB), 1, $32

    (And also with values ranging from $4 to $32 in increments of $4 just
    for the heck of it). But it doesn't seem to make any difference in the
    suicide, apart from the changed value of pc.

    The error is most likely because of the line (that's the only 7 around
    there):
    MOVL $7, 8(SP)

    But it doesn't complain about:
    MOVL $1, (SP)

    so I wonder...

    --
    Anant

  4. Re: [9fans] Hello Assembly

    > I did think that was the case earlier, so I tried:
    >
    > TEXT _main+0(SB), 1, $32
    >
    > (And also with values ranging from $4 to $32 in increments of $4 just
    > for the heck of it). But it doesn't seem to make any difference in the
    > suicide, apart from the changed value of pc.
    >
    > The error is most likely because of the line (that's the only 7 around
    > there):
    > MOVL $7, 8(SP)
    >
    > But it doesn't complain about:
    > MOVL $1, (SP)
    >
    > so I wonder...


    you seek:
    SUBL $value, SP

    - erik


  5. Re: [9fans] Hello Assembly

    > there might be more, but you haven't allocated a stack frame for the (system) call

    actually, there's probably enough space above your current location to (appear to) work, but a further error
    is hinted in the address given by the trap:

    8.out 1183: suicide: invalid address 0x7 in sys call pc=0x104e

    since 7 is your length value, you've also got an off-by-4 error in your stack offsets.


  6. Re: [9fans] Hello Assembly

    > actually, there's probably enough space above your current location to (appear to) work, but a further error
    > is hinted in the address given by the trap:
    >
    > 8.out 1183: suicide: invalid address 0x7 in sys call pc=0x104e
    >
    > since 7 is your length value, you've also got an off-by-4 error in your stack offsets.


    actually, i think there's something else wrong:

    acid: asm(_main)
    _main 0x00001020 SUBL $0x18,SP
    _main+0x3 0x00001023 MOVL $0x1,0x0(SP)
    _main+0xa 0x0000102a MOVL $string(SB),0x4(FP)
    _main+0x12 0x00001032 MOVL $0x7,0x8(FP)
    _main+0x1a 0x0000103a MOVL $0x0,0xc(FP)
    _main+0x22 0x00001042 MOVL $0x0,0x10(FP)
    _main+0x2a 0x0000104a MOVL $0x33,AX
    _main+0x2f 0x0000104f INTB $0x40
    _main+0x31 0x00001051 ADDL $0x18,SP
    _main+0x34 0x00001054 RET

    why does the
    for the equivalent c "pwrite(1, string, 7, 0)" i get this

    acid: asm(main)
    main 0x00001020 SUBL $0x18,SP
    main+0x3 0x00001023 MOVL $0x1,AX
    main+0x8 0x00001028 MOVL AX,0x0(SP)
    main+0xb 0x0000102b MOVL string(SB),AX
    main+0x11 0x00001031 MOVL AX,0x4(SP)
    main+0x15 0x00001035 MOVL $0x7,AX
    main+0x1a 0x0000103a MOVL AX,0x8(SP)
    main+0x1e 0x0000103e MOVL $0x0,0xc(SP)
    main+0x26 0x00001046 MOVL $0x0,0x10(SP)
    main+0x2e 0x0000104e CALL pwrite(SB)
    main+0x33 0x00001053 ADDL $0x18,SP
    main+0x36 0x00001056 RET

    - erik


  7. Re: [9fans] Hello Assembly

    > acid: asm(_main)
    > _main 0x00001020 SUBL $0x18,SP
    > _main+0x3 0x00001023 MOVL $0x1,0x0(SP)
    > _main+0xa 0x0000102a MOVL $string(SB),0x4(FP)
    > _main+0x12 0x00001032 MOVL $0x7,0x8(FP)
    > _main+0x1a 0x0000103a MOVL $0x0,0xc(FP)
    > _main+0x22 0x00001042 MOVL $0x0,0x10(FP)
    > _main+0x2a 0x0000104a MOVL $0x33,AX
    > _main+0x2f 0x0000104f INTB $0x40
    > _main+0x31 0x00001051 ADDL $0x18,SP
    > _main+0x34 0x00001054 RET
    >
    > why does the


    .... SP in the .s get changed to FP unless the offset is 0?

    - erik


  8. Re: [9fans] Hello Assembly

    On Sat, 09 Feb 2008 09:44:21 -0000, Anant Narayanan wrote:

    > Hi,
    >
    > I'm trying to make a Hello World program in assembly without depending
    > on libc. Here's what I have so far:
    >
    > DATA string+0(SB)/7, $"Hello\n\z"
    > GLOBL string+0(SB), $7
    >
    > TEXT _main+0(SB), 1, $0
    >
    > // first arg; $1 = stdout
    > MOVL $1, (SP)
    > // second arg, address of string
    > MOVL $string+0(SB), 4(SP)
    > // third arg, $7 = length of string
    > MOVL $7, 8(SP)
    > // fourth argument, -1LL (vlong offset)
    > MOVL $-1, 12(SP)
    > MOVL $-1, 16(SP)
    >
    > // use pwrite syscall
    > MOVL $51, AX
    > INT $64
    > RET
    >


    No two cents from me. Just posted this to say that AT&T syntax is
    "sickly." I suppose Anant Narayanan is assembling these using 8a, and
    given the "symptoms" (the syntax, that is) 8a must be using AT&T syntax.I
    am wondering if there is an Intel syntax assembler for Plan 9 (something
    akin to Netwide Assembler or yet better Flat Assembler).

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

  9. Re: [9fans] Hello Assembly

    I would like to hear what Rob or others have to say about the
    assembler syntax, but I actually like the syntax for the following
    reason. You only have to remember one syntax and not ten different
    ones. I would think, given that the structure of the compiler/loader
    is very non-traditional, it would be difficult to provide all the
    pseudo operations that the various native assemblers would require.

    Best thing is not to write a lot of assembler code.




    > On Sat, 09 Feb 2008 09:44:21 -0000, Anant Narayanan wrote:
    >
    >> Hi,
    >>
    >> I'm trying to make a Hello World program in assembly without depending
    >> on libc. Here's what I have so far:
    >>
    >> DATA string+0(SB)/7, $"Hello\n\z"
    >> GLOBL string+0(SB), $7
    >>
    >> TEXT _main+0(SB), 1, $0
    >>
    >> // first arg; $1 = stdout
    >> MOVL $1, (SP)
    >> // second arg, address of string
    >> MOVL $string+0(SB), 4(SP)
    >> // third arg, $7 = length of string
    >> MOVL $7, 8(SP)
    >> // fourth argument, -1LL (vlong offset)
    >> MOVL $-1, 12(SP)
    >> MOVL $-1, 16(SP)
    >>
    >> // use pwrite syscall
    >> MOVL $51, AX
    >> INT $64
    >> RET
    >>

    >
    > No two cents from me. Just posted this to say that AT&T syntax is
    > "sickly." I suppose Anant Narayanan is assembling these using 8a, and
    > given the "symptoms" (the syntax, that is) 8a must be using AT&T syntax. I
    > am wondering if there is an Intel syntax assembler for Plan 9 (something
    > akin to Netwide Assembler or yet better Flat Assembler).
    >
    > --
    > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



  10. Re: [9fans] Hello Assembly

    On Feb 9, 2008 8:17 AM, Brantley Coile wrote:
    > I would like to hear what Rob or others have to say about the
    > assembler syntax, but I actually like the syntax for the following
    > reason.


    if you love assembly code, the assembler on Plan 9 is not great.

    If you love assembly code, you are in need of a CAT scan in my view.

    The v6 manual entry for as called assembly code "the ultimate dead
    language". If only that had been true.

    gcc and friends have made the world safe for assembly, and there is more
    assembly in use than ever.

    Writing assembly code should be as painful as possible. Plan 9
    succeeds in that regard. It's a good thing in my view.

    ron

  11. Re: [9fans] Hello Assembly

    > if you love assembly code, the assembler on Plan 9 is not great.
    >
    > If you love assembly code, you are in need of a CAT scan in my view.
    >
    > The v6 manual entry for as called assembly code "the ultimate dead
    > language". If only that had been true.
    >
    > gcc and friends have made the world safe for assembly, and there is more
    > assembly in use than ever.
    >
    > Writing assembly code should be as painful as possible. Plan 9
    > succeeds in that regard. It's a good thing in my view.
    >
    > ron


    i don't think that it follows from the fact that assembly language
    is inappropriately used in a lot of linux software that it should
    be as hard to use as possible on plan 9.

    there are some tasks that must be in assembly language. and it's
    a good tool for learning how the machine works. sometimes, like
    on really small controllers, it's the best tool for the job.

    just my 2ยข.

    - erik

    p.s. relax ron. i promise not to rewrite plan 9 in assembly language.


  12. Re: [9fans] Hello Assembly

    > On Feb 9, 2008 8:17 AM, Brantley Coile wrote:
    >> I would like to hear what Rob or others have to say about the
    >> assembler syntax, but I actually like the syntax for the following
    >> reason.

    >
    > if you love assembly code, the assembler on Plan 9 is not great.
    >
    > If you love assembly code, you are in need of a CAT scan in my view.
    >
    > The v6 manual entry for as called assembly code "the ultimate dead
    > language". If only that had been true.
    >
    > gcc and friends have made the world safe for assembly, and there is more
    > assembly in use than ever.
    >
    > Writing assembly code should be as painful as possible. Plan 9
    > succeeds in that regard. It's a good thing in my view.
    >
    > ron


    I'm afraid Plan 9 fails in this reguard. Here's some Oberon code
    to make the pread call. (This is NOT Wirth's compiler, but Paul Reed's.
    He agrees with you about make assembly programming as hard as possible.)

    PROCEDURE syspread(poshi, poslo: LONGINT;
    VAR buf: ARRAY OF CHAR; fd: LONGINT): LONGINT;
    BEGIN
    (*
    18: return value
    14: poshi
    10: poslo
    0c: buflen
    08; buf ptr
    04: fd
    00: RA
    *)

    SYSTEM.CODE(
    0C9H, (* leave *)
    06AH, 50, (* pushb $close *)
    058H, (* pop eax *)
    0CDH, 040H, (* int $40h *)
    089H,084H,024H,018H,000H,000H,000H, (* movl eax, 18h(esp) *)
    0C2H, 14H, 0 (* ret $14h *)
    )
    END syspread;



  13. Re: [9fans] Hello Assembly

    >> No two cents from me. Just posted this to say that AT&T syntax is
    >> "sickly." I suppose Anant Narayanan is assembling these using 8a, and
    >> given the "symptoms" (the syntax, that is) 8a must be using AT&T syntax. I


    the assembler is really just a front-end to the loader, and nothing to do with `at&t syntax'.
    there are some odd aspects to thompson syntax as it is (data loading for instance,
    and the <>) but generally i agree with brantley. some weeks i'm working with several processors,
    even several a day, and the `native' (in what sense? does the processor implement them?)
    assemblers typically differ in operand order, basic mnemonics (l/st vs mov), and other conventions,
    whereas the ?a family is uniformly data flow, and tends to use similar instructions for
    similar things. i find it much easier moving from platform to platform with it.


  14. Re: [9fans] Hello Assembly


    On 09-Feb-08, at 4:04 PM, erik quanstrom wrote:
    > actually, i think there's something else wrong:
    > for the equivalent c "pwrite(1, string, 7, 0)" i get this


    I don't get FP for `8c -S hello.c' (which just calls pwrite as you
    describe). This is what I get:

    TEXT main+0(SB), 0, $32
    MOVL $.string<>+0(SB), CX
    MOVL $1, AX
    MOVL AX, (SP)
    MOVL CX, 4(SP)
    MOVL $7, AX
    MOVL AX, 8(SP)
    MOVL $-1, 12(SP)
    MOVL $-1, 16(SP)
    CALL ,pwrite+0(SB)
    RET ,
    DATA .string<>+0(SB)/8, $"Hello\n\z\z"
    GLOBL .string<>+0(SB), $8
    END ,

    Replicating the exact program - with just three changes: moving the
    DATA/GLOBL statement to the top, changing main to _main and replacing
    CALL with MOVL $64, AX and INT $64 - still results in the suicide.

    Puzzling.

    --
    Anant

  15. Re: [9fans] Hello Assembly

    > actually, there's probably enough space above your current location
    > to (appear to) work, but a further error
    > is hinted in the address given by the trap:
    >
    > 8.out 1183: suicide: invalid address 0x7 in sys call pc=0x104e
    >
    > since 7 is your length value, you've also got an off-by-4 error in
    > your stack offsets.


    In which direction, 4(SP) or -4(SP)?
    Both don't work, it still suicides but at different 'invalid
    addresses' and pc values

    --
    Anant

  16. Re: [9fans] Hello Assembly

    > If you love assembly code, you are in need of a CAT scan in my view.

    chances are the CAT scanner was programmed in assembler !




  17. Re: [9fans] Hello Assembly

    On Sat, 09 Feb 2008 17:32:26 -0000, Charles Forsyth
    wrote:

    > the assembler is really just a front-end to the loader, and nothing to
    > do with `at&t syntax'.


    For the first ten minutes after reading that, I was wondering what the
    comment meant. Then I remembered in Plan 9 speak the loader means the
    linker. So, I gather from what you have pointed out that 8a assembly is
    actually some sort of intermediate language like MSIL (or GNU's version of
    AT&T assembly for the GCC backend).

    > and the <>) but generally i agree with brantley. some weeks i'm working
    > with several processors,
    > even several a day, and the `native' (in what sense? does the processor
    > implement them?)
    > assemblers typically differ in operand order, basic mnemonics (l/st vs
    > mov), and other conventions,
    > whereas the ?a family is uniformly data flow, and tends to use similar
    > instructions for
    > similar things. i find it much easier moving from platform to platform
    > with it.


    That is probably because assembly was never intended for moving from one
    platform to another all the time, but for squeezing the most out of a
    given platform whose nooks and crannies you ken well.

    The notion of a "native" assembler sounds strange to me. Netwide (nasm) is
    known for being available on many software/hardware platforms with similar
    syntax (the Intel syntax) all over. Of course, the instruction sets are
    far from identical but the syntax is still (almost) the same. Register
    access, for instance, is done by simply naming the register in an
    instruction:

    mov eax, 00h

    Compared to the AT&T syntax (for GNU assembler):

    movl $0x0, %eax

    (three extra keystrokes to get the same op-code)

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

  18. Re: [9fans] Hello Assembly

    * Eris Discordia (eris.discordia@gmail.com) wrote:
    > That is probably because assembly was never intended for moving from one
    > platform to another all the time, but for squeezing the most out of a
    > given platform whose nooks and crannies you ken well.


    Nah, that's only what the intel assembler is for. Originally, assembly
    language was devised for not having to type in machine opcodes directly.
    With the advent of high level programming laguages its main merit became the
    ability to write those bits of operating system code that just cannot be
    written in a high level language. (Research) Unix was influential in that
    regard and showed that those parts are really small (and should be).

    Martin


  19. Re: [9fans] Hello Assembly

    On Feb 10, 2008, at 3:56 AM, Martin Neubauer wrote:

    > (Research) Unix was influential in that
    > regard and showed that those parts are really small (and should be).
    >
    > Martin


    I am working on rewriting an operating system that avoids this
    philosophy for the purpose of teaching assembly language. So far, I
    have 2% of the code (I started a rewrite), and I don't know if my
    code is 100% right.



  20. Re: [9fans] Hello Assembly

    > I am working on rewriting an operating system that avoids this
    > philosophy for the purpose of teaching assembly language. So far, I
    > have 2% of the code (I started a rewrite), and I don't know if my
    > code is 100% right.


    There's an OS (complete with a Window Manager, IDE, Web Browser, and
    even some games) written entirely in assembly: http://www.menuetos.net/

    Everyone has 24 hours in a day, but some use it more than others

    --
    Anant

+ Reply to Thread
Page 1 of 2 1 2 LastLast