-
Notifications
You must be signed in to change notification settings - Fork 0
get info registers #1
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
Open
i4ki
wants to merge
4
commits into
master
Choose a base branch
from
xxx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** function definitions */ | ||
|
|
||
| void printRegs32(Reg*); | ||
| void printRegs16(Reg*); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(®); | ||
|
|
||
| printRegs32(®); | ||
| printRegs16(®); | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 =)
There was a problem hiding this comment.
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. ;)
There was a problem hiding this comment.
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
asmdirective. C and asm was cool before gcc introduced the extended inline asm thing...There was a problem hiding this comment.
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
asmdirectives... 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
asmdirective best to leave it alone inside the function (as I did). Never put C and asm together inside same function...