Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions xxx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
TARGET = xxx

all: clean $(TARGET)

$(TARGET):
smlrcc main.c regs.c -I./include -o $(TARGET)

debug:
gdb -ex "set architecture i386:intel" \
-ex "set disassembly-flavor intel" \
-ex "layout asm" -ex "layout regs" \
-ex "br *0x80490b0" \
-ex "set startup-with-shell off" \
./xxx

clean:
rm -f $(TARGET)
23 changes: 23 additions & 0 deletions xxx/dat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* common data types
*/

#include <stdint.h>

/* basic types */
typedef int32_t i32;
typedef int16_t i16;
typedef int8_t i8;

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

/* Reg hold processor registers */
typedef struct {
i32 eax, ebx, ecx, edx;
i32 eip, ebp, esi, edi;
i32 esp;

i16 cs, ds, es, fs, gs;
} Reg;
4 changes: 4 additions & 0 deletions xxx/fns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** function definitions */

void printRegs32(Reg*);
void printRegs16(Reg*);
14 changes: 14 additions & 0 deletions xxx/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include "dat.h"
#include "fns.h"

int main() {
Reg reg;

newreg(&reg);

printRegs32(&reg);
printRegs16(&reg);

return 0;
}
124 changes: 124 additions & 0 deletions xxx/regs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* get register info
*/

#include <stdint.h>
#include "dat.h"

#define LSW(r32) (r32 & 0xffff) /* least significant word */
#define MSW(r32) (i16(r32 >> 4)) /* most significant word */

void newreg(Reg *reg) {
reg->eax = eax();
reg->ebx = ebx();
reg->ecx = ecx();
reg->edx = edx();
reg->eip = eip();
reg->ebp = ebp();
reg->esi = esi();
reg->edi = edi();

reg->esp = esp();

reg->cs = cs();
reg->ds = ds();
reg->es = es();
reg->fs = fs();
reg->gs = gs();
}

void printRegs32(Reg *reg) {
printf("eax=%08x\tesp=%08x\n", reg->eax, 0);
printf("ebx=%08x\tebp=%08x\n", reg->ebx, reg->ebp);
printf("ecx=%08x\tesi=%08x\n", reg->ecx, reg->esi);
printf("edx=%08x\tedi=%08x\n", reg->edx, reg->edi);
printf("eip=%08x\n", reg->eip);
}

void printRegs16(Reg *reg) {
printf("ax=%04x\tsp=%04x\n", LSW(reg->eax) , LSW(reg->esp));
printf("bx=%04x\tbp=%04x\n", LSW(reg->ebx) , LSW(reg->ebp));
printf("cx=%04x\tsi=%04x\n", LSW(reg->ecx) , LSW(reg->esi));
printf("dx=%04x\tdi=%04x\n", LSW(reg->edx) , LSW(reg->edi));

printf("cs=%04x\tds=%04x\n", reg->cs , reg->ds);
printf("es=%04x\tfs=%04x\n", reg->es, reg->fs);
printf("gs=%04x\n", reg->gs);

}

/* 32bit registers */

i32 eax() {
/* by calling convention */
}

i32 ebx() {
asm("mov eax, ebx");
}

i32 ecx() {
asm("mov eax, ecx");
}

i32 edx() {
asm("mov eax, edx");
}

i32 eip() {
asm("call __next\n"
"__next: pop eax");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's awesome.... i didn't know asm allowed to define function like that =)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That inlined assembly works as a label definition. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the old school asm directive. C and asm was cool before gcc introduced the extended inline asm thing...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GCC dislikes that because people unaware of calling convention could mess things up trying to set local C variables from inside asm directives... To do that, you need to set things directly to ebp+8, ebp+16, and so on (arch dependent) and this could change depending on compiler optimizations (and compiler flags). To pack local variables, the order of variables in the stack could change also.
Then, to use oldish asm directive best to leave it alone inside the function (as I did). Never put C and asm together inside same function...


i32 esp() {
asm("mov eax, esp");
}

i32 ebp() {
asm("mov eax, ebp");
}

i32 esi() {
asm("mov eax, esi");
}

i32 edi() {
asm("mov eax, edi");
}

/* 16bit registers */
i16 ax() {
asm("mov bx, ax\n"
"xor eax, eax\n"
"mov ax, bx");
}

i16 bx() {
asm("xor eax, eax\n"
"mov ax, bx");
}

i16 cs() {
asm("xor eax, eax\n"
"mov ax, cs");
}

i16 ds() {
asm("xor eax, eax\n"
"mov ax, ds");
}

i16 es() {
asm("xor eax, eax\n"
"mov ax, es");
}

i16 fs() {
asm("xor eax, eax\n"
"mov ax, fs");
}

i16 gs() {
asm("xor eax, eax\n"
"mov ax, gs");
}