hi
how can download a file (for example file.text) from an url in assembly language
programing in mac osx or linux

Two main methods:

  • Plug into an existing utility (wget or curl for example)
  • Link against an existing library with the level of abstraction you need

Hi
Thanks

I can download file with sending command to bash with using execve syscall and call Curl

But I want download file from my program and not use another program or process.

I think I'll be using your second suggestion.
But I don't know how call funcion in another library

I can call printf or puts of c function in my assembly code , but i can not call curl function from libcurl.dylib

How can call it's function?

You have to first declare it as an external function using something like extern function_name_here (the exact syntax will vary from assembler to assembler). Then, you need to instruct your linker to link against libcurl.dylib. What assembler/toolchain are you using?

It really is not hard at all to use libcurl. You read the docs pass some parameters and call the functions. The following simple code will get googles robots.txt file and display it in the terminal.

It is 64-bit using NASM tested on Linux

%define CURL_GLOBAL_SSL (1<<0)
%define CURL_GLOBAL_WIN32 (1<<1)
%define CURL_GLOBAL_ALL (CURL_GLOBAL_SSL|CURL_GLOBAL_WIN32)
%define CURL_GLOBAL_NOTHING 0
%define CURL_GLOBAL_DEFAULT CURL_GLOBAL_ALL

%define CURLOPT_URL 10002
%define CURLOPT_FOLLOWLOCATION 52
%define CURLOPT_WRITEHEADER 10029
%define CURLOPT_WRITEDATA 10001

extern curl_easy_init, curl_easy_cleanup, curl_easy_setopt, curl_easy_perform
extern curl_global_init, curl_global_cleanup

extern exit, stdout

section .data
url         db  "http://www.google.com/robots.txt", 0

global main
section .text
main:

    mov     rdi, CURL_GLOBAL_ALL
    call    curl_global_init

    call    curl_easy_init
    mov     r15, rax

    mov     rdx, url
    mov     rsi, CURLOPT_URL
    mov     rdi, r15
    xor     rax, rax
    call    curl_easy_setopt

    mov     rdx, 1
    mov     rsi, CURLOPT_FOLLOWLOCATION
    mov     rdi, r15
    xor     rax, rax
    call    curl_easy_setopt

    mov     rdx, [stdout]
    mov     rsi, CURLOPT_WRITEDATA
    mov     rdi, r15
    xor     rax, rax
    call    curl_easy_setopt

    mov     rdi, r15
    call    curl_easy_perform

    mov     rdi, r15
    call    curl_easy_cleanup

    call    curl_global_cleanup

    mov     rdi, 0
    call    exit

Error handling/checking has been left out.

and the makefile:

APP=download_file

all: $(APP) clean

$(APP): $(APP).o
    gcc -g -o $(APP) $(APP).o -lcurl

$(APP).o: $(APP).asm
    nasm -f elf64 $(APP).asm -F dwarf

clean:
    rm $(APP).o
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.