hello to all
I want convert this code to assembly for mac osx intel 64 :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

//#define RCVBUFSIZE 4096

int main( int argc, char *argv[] ) {


    int sock, length;
    struct sockaddr_in server;
    char   *command;
    //  char  buffer[RCVBUFSIZE] ;
    //  FILE *fp;

    //csize = strlen( "/test/2.gif" );
    //command = malloc ( csize + 50 );

    /* build the request we are going to send */
    command="GET /test/2.gif HTTP/1.1\r\nHost: 10.1.1.187\r\n\r\n";



    /* create reliable stream socket */
    sock = socket ( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ;


    /* Construct the server address structure */
    memset ( &server, 0, sizeof( server ) );
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = 3103850762;
    server.sin_port = 20480;
    //  int x = sizeof(server);
    //  printf("%d",x);
    /* establish a connection to the server */
    connect ( sock, ( struct sockaddr * )&server, sizeof( server ));

    //  fp = fopen( "47.gif", "a" );

    length = strlen( command );
    //  printf("%d",length);
    sendto(sock, command, length, 0,(struct sockaddr * )&server, sizeof( server ));
    //  int init = 0;
    //  while( ( bytes = recv ( sock, buffer, sizeof( buffer ), 0 ) ) > 0 ) {
    //        
    //      char *p;
    //        if (init) {
    //            p = buffer;
    //        } else {
    //            if ((p = (char *)memmem (buffer, bytes, "\r\n\r\n", 4))) {
    //                p += 4; init = 1;
    //            } else if ((p = (char *)memmem (buffer, bytes, "\n\n", 2))) {
    //                p += 2; init = 1;
    //            } else {
    //                continue;
    //            }
    //        }
    //      bytes -= (p - buffer);
    //        fwrite (p, bytes, 1, fp);
    //        
    //  }

    //  printf("success\n");
    close(sock);
    //  fclose(fp);

    exit(0);

}

I write this , but I sendto return error , when I trace it , my code is :

section .data
command     db "GET /test/2.gif HTTP/1.1\r\nHost: 10.1.1.187\r\n\r\n", 0
; url                   db "http://172.16.207.153/test/2.gif", 0

global main
section .text

main:   

    ;Socket
    mov rdx, 6                  ; rdx = IPPROTO_TCP = 6
    mov rsi, 1                  ; rsi = AF_NET = 1 
    mov rdi, 2                  ; rdi = SOCK_STREAM = 2 
    mov rax, 0x2000061          ; socket syscall = 97
    syscall                     ; call socket(SOCK_STREAM, AF_NET, IPPROTO_TCP);
    mov r12, rax                ; Save the socket

;     xor  xmm0,xmm0
;       mov qword [rbp-64], xmm0 
;       mov byte [rbp-63], 2 
;       mov qword [rbp-60], 3103850762 
;       mov qword [rbp-62], 20480 
;       lea  r13, [rbp-64]




    ;Sock_addr
;   mov r13, 0xB901010A50000101 ; IP = FFFFFFFF, Port = 5C11(4444)
;     mov r9b, 0xFF               ; The sock_addr_in is + FF from where we need it
;     sub r13, r9                 ; So we sub 0xFF from it to get the correct value and avoid a null
;     push r13                    ; Push it on the stack
;     mov r13, rsp                ; Save the sock_addr_in into r13

    mov r13, 0xB901010A50000002 ; IP = 0A0101B9, Port = 50(80)
    push r13                    ; Push it on the stack
    mov r13, rsp                ; Save the sock_addr_in into r13

    ;Connect 
    mov rax, 0x2000062          ; connect syscall = 98
    mov rdi, r12                ; move the saved socket fd into rdi
    mov rsi, r13                ; move the saved sock_addr_in into rsi
    add rdx, 0x10               ; add 0x10 to rdx
    syscall                     ; call connect(rdi, rsi, rdx)

    ;sendto 
    mov rax, 0x2000085          ; connect syscall = 113
    mov rdi, r12                ; move the saved socket fd into rdi

;     mov  r14, command
;   push r14
;     mov  r14,rsp

    xor  r15,r15;
    push r15
    mov  r14, 0x6e5c725c
    push r14
    mov  r14, 0x6e5c725c3738312e
    push r14
    mov  r14, 0x312e312e3031203a
    push r14
    mov  r14, 0x74736f486e5c725c
    push r14
    mov  r14, 0x312e312f50545448
    push r14
    mov  r14, 0x206669672e322f74
    push r14
    mov  r14, 0x7365742f20544547
    push r14
    mov  r14,rsp

    mov  rsi, r14                ; move the saved sock_addr_in into rsi
    mov  rdx, 0x2e               ; add 0x10 to rdx 
    mov  rcx, 0     
    mov  r8, r13
    mov  r9, 0x10

    syscall                     ; call sendto(rdi, rsi, rdx)




    mov     rdi, r12
    mov     rax, 0x2000006 ; close
    syscall


    mov     rax, 0x2000001 ; exit
    mov     rdi, 0x0
    syscall

please help me .

Is there any particular reason you're trying to convert rather complex C-stuff to even more complex ASM stuff? If it works in C then hurray.

If you're really heading to convert it, you'll either end up rewring the whole TCP stack or link the C-functions into your ASM-project which makes the whole idea obsolete.

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.