Results 1 to 7 of 7

Thread: Assembly with nasm.

  1. #1
    Join Date
    Jul 2007
    Location
    India
    Beans
    449
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Assembly with nasm.

    I have just begun assembly and i am a noob.

    Can someone check this simple program to add 2 numbers from memory. I am getting a error when linking and i dont know what it means.

    section .data
    x: db 13d
    y: db 15d
    CPU 8086
    section .text
    global _start

    _start:
    mov bx, x
    mov bl, [x]
    add bl, [y]

    int 80h
    mov ax,1
    mov bx,0
    int 80h

    The error i get after ld is
    first.o: In function `_start':
    first.asm.text+0x2): relocation truncated to fit: R_386_16 against `.data'

    Why is this?
    Ultimately isn't the world round.

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Assembly with nasm.

    What are your "ld" options? Actually, I wonder if "ld" can actually be used to link 16-bit programs.

  3. #3
    Join Date
    Jul 2007
    Location
    India
    Beans
    449
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Assembly with nasm.

    I simply used ld -o name.name.o
    Ultimately isn't the world round.

  4. #4
    Join Date
    Jul 2007
    Location
    India
    Beans
    449
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Assembly with nasm.

    How do you change ld to make it work for a 16 bit instruction set??
    Ultimately isn't the world round.

  5. #5
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Assembly with nasm.

    Quote Originally Posted by i.mehrzad View Post
    I simply used ld -o name.name.o
    Can't be - "name.name.o" will be the name of the output file this way.

    Quote Originally Posted by i.mehrzad View Post
    How do you change ld to make it work for a 16 bit instruction set??
    ld will only build executables for modern operating systems, running in protected mode, defaulting to 32 bit instructions. So you will need to add "BITS 32" to your NASM program. This does not mean that you cannot use the 16-bit instructions, but you are forced to have your program running in protected mode, which did not exist for the 8086. Might work anyway, but memory addressing is different here.

    There has also been a project for a 16-bit "ld" linker, use google for finding that. Don't know if that works.

  6. #6
    Join Date
    Jul 2007
    Location
    India
    Beans
    449
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Assembly with nasm.

    Problem is that in college they are teaching masm, in native 8086 form.
    I dont want to get confused, i am anyway adapting to the variations in syntax.
    In order to simlify things i am using CPU 8086.

    Anyway, is nemiver a good replacement for codeview??
    Ultimately isn't the world round.

  7. #7
    Join Date
    Feb 2009
    Beans
    24

    Re: Assembly with nasm.

    You don't want "cpu 8086" in conjunction with "int 80h"!

    If you want to run dos code - which is apparently what they're teaching in college - look into "dosemu". It should allow you to run 8086 code.

    What you want for a Linux program is:

    ; nasm -f elf mygem.asm
    ; ld -o mygem mygem.o

    global _start ; tell ld about it

    section .text
    _start:

    mov eax, 1 ; __NR_exit
    mov bl, 42
    int 80h

    All that does is exit, but if you then type "echo $?", it'll print the "exit code"... which we said was 42.

    You can add code to add two numbers (your code is okay) and display the answer with "echo $?" - only good up to 255. If you want to print the answer... well, we print characters, not numbers. If you print the number 42, you'll see 'B', not '4' followed by '2', which is what you want. (true of dos, too, BTW) We print something like:

    mov edx, buffer length
    mov ecx, address of buffer
    mov ebx, 1 (stdout - the screen)
    mov eax, 4 ;__NR_write
    int 80h

    Note that ecx is the address of the text to print, not the number to print! (if you need help with this, there are lots of examples, ask...)

    If you need to practice with "college code" - dosemu - int 80h is not a drop-in replacement for int 21h - it just won't work!

    Best,
    Frank

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •