0

How to link this program?

Linux testPC 5.8.0-40-generic #45~20.04.1-Ubuntu SMP Fri Jan 15 11:35:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Code:

    global _start
    extern printf


    section     .text
_start:     mov         ebx, [esp]              ; get argc
            mov         [argc], ebx
            lea         eax, [esp + 4]          ; get argv pointer
            mov         [argv], eax
            cmp         ebx, 2
            jne         end
            
            mov         ebx, [esp + 8]
            mov         [filename], ebx
            mov         eax, 5
            mov         ecx, 0
            int         80h
            cmp         eax, 0
            jl          end
            mov         [fd], eax

read_loop:  mov         eax, 3
            mov         ebx, [fd]
            mov         ecx, c
            mov         edx, 1
            int         80h
            cmp         eax, 0
            jle         close

            mov         eax, [len]
            add         eax, 1
            mov         [len], eax
            mov         eax, 4
            mov         ebx, 1
            mov         ecx, c
            mov         edx, 1
            int         80h
            jmp         read_loop

close:      mov         eax, 4
            mov         ebx, 1
            mov         byte [c], 10
            mov         ecx, c
            mov         edx, 1
            int         80h
            mov         eax, 6
            int         80h

end:        jmp         put_nbr
exit:       mov         eax, 4
            mov         ebx, 1
            mov         byte [c], 10
            mov         ecx, c
            mov         edx, 1
            mov         eax, 1
            mov         ebx, 0
            int         80h

put_nbr:    mov         eax, [len]
            push        eax
            push        fmt
            call        printf
            add         esp, 8
            jmp         put_nbr

    section     .data
argc:       dd  0
argv:       dd  0
filename:   dd  0
fd:         dd  0
c:          db  0
len:        dd  0
fmt:        db  "Size of file: %d",10,0

Compile and link command:

nasm -f elf example.asm && ld -m elf_i386 -L /lib32/ example.o -lc-2.31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.