Question

I'm looking to understand how to do basic input/output to write a text based game in x86 assembly, simply for the sake of learning of the instruction set and the internals.

I don't want to use stdlib.h or stdio.hin my assembly code unless it involves something complicated like printf, which I'd then call from assembly.

I'd like to learn how to emulate enums and structs if possible. AFAIK writing functions and sending them params is just a case of pushing/popping specific registers on and off the stack and/or manipulating esp using multiples of 4.

How would I do this in x86 using intel syntax?

Update

Sorry, I forgot to specify the target - I'm using Linux.


Example Code - Function prototype implementation omitted for sake of brevity

#include <stdio.h>
#include <stdlib.h>

typedef enum __weapon_type__ {

    weapon_type_sword = 1, 
    weapon_type_spear = 2, 
    weapon_type_knife = 3

} weapon_type;

typedef struct __weapon__ {

    unsigned int damage;
    char*        name;
    weapon_type  type;  

} weapon;

weapon* weapon_create( int damage, char* name, weapon_type type );

void putline( const char* msg );

int main( int argc, char** argv )
{
    unsigned int weapon_selection, weapon_damage;
    weapon_type weptype;
    weapon* player_weapon = NULL;
    char* weapon_name = NULL;

    putline( "Choose your weapon type:\t" );
    putline( "(1) Sword" );
    putline( "(2) Spear" );
    putline( "(3) Knife" );

    while ( weapon_selection > 3 || weapon_selection < 1 )
    {
        scanf( "%u", &weapon_selection );

        switch( weapon_selection )
        {
            case 1:
                weptype = weapon_type_sword;
                break;
            case 2: 
                weptype = weapon_type_spear;
                break;
            case 3:
                weptype = weapon_type_knife;
                break;
            default:
                putline( "ERROR! Please select options 1 - 3\t" );
                break;
        }
    }

    /*do the same thing for weapon_damage and weapon_name, etc.
      Then ask for player name, type of character, blah blah blah.
    */

    player_weapon = weapon_create( weapon_damage, weapon_name, weptype );

    return 0;
}
Was it helpful?

Solution

On Linux applications, all I/O is done thru syscalls (which from the application point of view, are elementary operations, usually implemented thru a mode-switching machine instruction like SYSENTER, SYSCALL, INT ...). I suggest reading the linux assembly howto.

See also this and that answers.

Look at the output of the compiler, using gcc -Wall -fverbose-asm -O -S your-c.c. You'll learn that a call to printf or to any C function is (on x86 in 32 bits) pushing arguments on the stack (on x86-64 some arguments are passed in registers). There are some calling conventions, e.g. those defined (for x86-64) in the x86-64 ABI (a similar document exist for other architectures). A C enum is just a way to define some constants. A C struct is just an aggregate i.e. a memory zone with data inside etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top