-2

Ok so basically I was just writing a C program to build my object files and then create executeables from them by using nasm and ld respectively

The program I wrote makes the correct calls to nasm and ld but I either compile fine with -f win32/win64 ( I'm on a 64 bit windows 7 machine ) or fail with the other options which is fine though... right? If the program compiles and creates the exe it runs and immediately crashes without printing the Hello World message. I'd Really like to jump into assembly. Some Help ?

section .text
    global _start   ;must be declared for linker (ld)
_start:             ;tells linker entry point
    mov edx,len     ;message length
    mov ecx,msg     ;message to write
    mov ebx,1       ;file descriptor (stdout)
    mov eax,4       ;system call number (sys_write)
    int 0x80        ;call kernel

    mov ah,00
    int 16h

    mov eax,1       ;system call number (sys_exit)
    int 0x80        ;call kernel

    section .data
    msg db 'Hello, world!', 0xa  ;our dear string
    len equ $ - msg     ;length of our dear string

I also happen to have a kali system ; I don't suppose I could compile for both operating systems without using Wine?

So my C program is working nicely! I can't find any examples of code to assemble though. Well I can... but it all fails. Does anyone have a link?

    #include <stdio.h>
    #include <stdlib.h>
    #include <CustomHeader_Small.h>
    void Assemble(void);
    void PrintMenu(void);
    void LoadOptions(void);
    void SaveOptions(void);
    char TempBuff[255];
    char Format[30];

void SaveOptions(void)
{
  FILE *Source = fopen("Settings.ini","w");
    if(Source)
    {
     printf("%s","Enter A Format Type ->");
     scanf("%s",Format);                    //Save Format
     fprintf(Source,"Format:%s",Format);
     fclose(Source);
     puts("Settings Updated!");
    LoadOptions();
    }
return;
}

void LoadOptions(void)
{
    FILE *Source = fopen("Settings.ini","r");
    if(Source)
    {
     char ch;
     int i;
     char Line[50];
     fscanf(Source,"%s",Line);
     CCopy(Line,CPos(Line,":",0)+1,CLen(Line),Format,0,1);
     free(Line);
    }
    else
    {
        Source = fopen("Settings.ini","w");
        fprintf(Source,"%s","Format:Win32");
        fclose(Source);
    }
    PrintMenu();
    LoadOptions();
    return;
}
void PrintMenu(void)
{
printf("%s","Menu:\n________\n1.) [C]reate A New Project.\n2.) [O]pen A Project.\n3.) [A]ssemble A Project.\n4.) [E]dit Settings\n");
printf("%s","5.) [Q]uit\n");
return;
}
void Assemble(void)
{
    char *File=malloc(256);
    printf("Note : Compiling In %s Mode\n",Format);
    printf("%s","Enter A Project Name -> ");
    scanf("%s",File);
    char *Command;
    int ch;
    while((ch=getchar())!='S')
    {
    Command=malloc(1024);
    strcpy(Command,"H:\\Users\\Grim\\AppData\\Local\\nasm\\nasm.exe -f ");
    strcat(Command,Format);
    strcat(Command," ");
    strcat(Command,File);
    strcat(Command,"\\");
    strcat(Command,File);
    strcat(Command,".asm ");
    strcat(Command,"-o ");
    strcat(Command,File);
    strcat(Command,"\\");
    strcat(Command,File);//This Creates The Object File Using Nasm ( Not Just Yet But Were Well On Our Way!
    strcat(Command,".o");

    system(Command); //Calls Nasm.

    free(Command);
    Command=malloc(1024);
    strcpy(Command,"H:\\MinGW\\bin\\ld.exe ");
    strcat(Command,File);
    strcat(Command,"\\");
    strcat(Command,File);
    strcat(Command,".o ");
    strcat(Command,"-o ");
    strcat(Command,File);//This Creates The Executable File Using Nasm ( Not Just Yet But Were Well On Our Way!
    strcat(Command,"\\");
    strcat(Command,File);
    strcat(Command,".exe");


    system(Command); //Calls Nasm.

    free(Command);
    puts("Press Enter To Compile Again But Enter An [S] Followed By Enter To [S]top.");
    }

    free(File);
    puts("NasmWrapper Assembly Done!");
    printf("%s","\n\n\n");
    PrintMenu();
    return;
}

int main()
{
    LoadOptions();
    char ch;
    while((ch = getchar())!='Q')
    {
    if(ch=='A') Assemble();
    if(ch=='E') SaveOptions();

    }
    return 0;
}

Also any comments on the C program would be nice :D Thanks for explaining how to use the [Code] thing.

1 Answers1

2

Your assembly program, apart from that odd int 16h* is specifically for Linux (32-bit Linux, to be more precise). int 0x80 is the way you invoke one of the Linux kernel system calls.

Windows doesn't do it this way. Instead you call the Windows API or the C standard library.

This OS-specific variation is one of the reasons it is good to use a higher level language rather than assembly.

If you want to play with assembly, my recommendation would be to decide on which OS you want to start with, and use that exclusively to begin with. Find some tutorials (there are lots for Linux and Windows) and get started. Once you have got it working for one OS, try it for another.

* int 16h calls the BIOS from DOS. This won't work in Linux.

Community
  • 1
  • 1
Tony
  • 1,645
  • 1
  • 10
  • 13