Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get info registers #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 ./xxx \
-ex "target remote localhost:1234" \
-ex "set architecture i80386" \
-ex "set disassembly-flavor intel" \
-ex "layout asm" -ex "layout regs" \
-ex "break *0x80490b0"

clean:
rm -f $(TARGET)
11 changes: 11 additions & 0 deletions xxx/include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* common data 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;
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>

void printRegs() {
printf("eax=%08x\tesp=%08x\n", eax(), esp());
printf("ebx=%08x\tebp=%08x\n", ebx(), ebp());
}

int main() {
printf("xxx v0.0.1\n");

printRegs();

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

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

/* 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("__geteip: mov eax, [esp]\n"
" ret\n"
"call __geteip");
}

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");
}