我正在寻找了解如何在 x86 汇编中编写基于文本的游戏的基本输入/输出,只是为了学习指令集和内部结构。
我不想在我的汇编代码中使用stdlib.h
or stdio.h
,除非它涉及一些复杂的东西,比如printf
,然后我会从汇编中调用它。
如果可能的话,我想学习如何模拟枚举和结构。AFAIK 编写函数并向它们发送参数只是将特定寄存器推入和移出堆栈和/或esp
使用 4 的倍数进行操作的情况。
我将如何使用 intel 语法在 x86 中执行此操作?
更新
抱歉,我忘了指定目标 - 我使用的是 Linux。
示例代码 - 为简洁起见省略了函数原型实现
#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;
}