I create a self exception handler by using assembly in c file by this code:
__asm
{
pushad
mov esi, offset Handler
push esi
push dword ptr fs:[0]
mov dword ptr fs:[0], esp
push u32Param
call pFunc;
jmp NoException
Handler:
mov esp, [esp + 8]
pop dword ptr fs:[0]
add esp, 4
popad
mov eax, 0x80000000
jmp ExceptionHandled
NoException:
pop dword ptr fs:[0]
add esp, 40
ExceptionHandled:
}
This code is simple exception handling in assembly.
This code works in new created vc project.But in my project it make an exception and vc says that there is an invalid exception handler.
Any Suggestion?
If you are following this article, which it seems you are, then why aren't you properly restoring the stack as shown in the code there?
The code in the article:
18 NoException&;Handler: ;;No Exception Occured
19 pop dword ptr fs:[0] ;;Restore Old Exception Handler
20 add esp, 32 + 4 ;;ESP value before SEH was set. 32 for pushad and ...
21 ExceptionHandled&;Handler: ;;...4 for push offset Handler. (No Restore State)
22 ;;Exception has been handled, or no exception occured
Your code:
NoException:
pop dword ptr fs:[0]
add esp, 8
ExceptionHandled:
32 in that code is to undo pushad, 4 is to undo push esi. Why do you have 8? 32 + 4 ≠ 8.
If that's how you want to remove u32Param from the stack (in case pFunc doesn't do it for you), then you should do it between these two lines:
call pFunc;
add esp, 4
jmp NoException
My version:
// file: tst.c
// compile with Open Watcom C/C++ 1.9: wcl386.exe /q /we /wx tst.c
// ditto with debug info: wcl386.exe /q /we /wx /d2 tst.c
#include <stdio.h>
unsigned __stdcall func(volatile unsigned* p)
{
return *p;
}
unsigned blah(unsigned (__stdcall *pFunc)(volatile unsigned*), volatile unsigned* u32Param)
{
unsigned result = 0;
__asm
{
pushad
// mov esi, offset Handler // Open Watcom C/C++ says Handler is undefined
// push esi
// lea eax, blah
// add eax, Handler - blah // this difference doesn't come out correct with Open Watcom C/C++
// add eax, 78 // 78 is Handler - blah // this is unreliable
// push eax
push 0xFDCB4321
jmp GetHandlerAddr
GotHandlerAddr:
pop eax
add esp, 4
push eax
push dword ptr fs:[0]
mov dword ptr fs:[0], esp
push u32Param
call dword ptr [pFunc]
jmp NoException
GetHandlerAddr:
call Handler // this will place &Handler on the stack
Handler:
cmp dword ptr [esp + 4], 0xFDCB4321
je GotHandlerAddr
mov esp, [esp + 8]
pop dword ptr fs:[0]
add esp, 4
popad
mov eax, 0x80000000
jmp ExceptionHandled
NoException:
pop dword ptr fs:[0]
add esp, 32 + 4
ExceptionHandled:
mov result, eax
}
return result;
}
int main(void)
{
volatile unsigned n = 0x113355AA;
printf("%08X\n", func(&n));
printf("%08X\n", blah(&func, &n));
printf("%08X\n", blah(&func, (volatile unsigned*)0));
printf("%08X\n", blah(&func, (volatile unsigned*)0));
return 0;
}
Output:
113355AA
113355AA
80000000
80000000
Related
C code
#include <stdio.h>
int fibonacci(int);
int main()
{
int x = fibonacci(3);
printf("Fibonacci is : %d",x);
return 0;
}
Assembly
section .text
global fibonacci
fibonacci:
push ebp;
mov ebp, esp;
; initialize
mov dword [prev], 0x00000000;
mov dword [cur], 0x00000001;
mov byte [it], 0x01;
mov eax, dword [ebp + 8]; // n = 3
mov byte [n], al;
getfib:
xor edx,edx;
mov dl, byte [n];
cmp byte [it] , dl;
jg loopend;
mov eax,dword [prev];
add eax, dword [cur];
mov ebx, dword [cur];
mov dword [prev], ebx;
mov dword [cur] , eax;
inc byte [it];
jmp getfib;
loopend:
mov eax, dword [cur];
pop ebp;
ret;
section .bss
it resb 1
prev resd 1
cur resd 1
n resb 1
I was trying to run this assembly function in C code and on debugging , i saw that value in variable x in C code is right but there is some error coming when i use the printf function
Need Help on it
Command to compile:
nasm -f elf32 asmcode.asm -o a.o
gcc -ggdb -no-pie -m32 a.o ccode.c -o a.out
Click Below Pictures if they seem blurred
Below is debug before printf execute
Below is after printf execute
Your code does not preserve the ebx register which is a callee-preserved register. The main function apparently tries to do some rip-relative addressing to obtain the address of the format string for printf using ebx as a base register. This fails because your code overwrote ebx.
To fix this issue, make sure to save all callee-saved registers before you use them and then restore their value on return. For example, you can do
fibonacci:
push ebp
mov ebp, esp
push ebx ; <---
...
pop ebx ; <---
pop ebp
ret
I wrote this classic function : (in 32-bit mode)
void ex(size_t a, size_t b)
{
size_t c;
c = a;
a = b;
b = c;
}
I call it inside the main as follows :
size_t a = 4;
size_t b = 5;
ex(a,b);
What I was expecting from the assembly code generated when entering the function is something like this :
1-Push the values of b and a in the stack : (which was done)
mov eax,dword ptr [b]
push eax
mov ecx,dword ptr [a]
push ecx
2-Use the values of a and b in the stack :
push ebp
mov ebp, esp
sub esp, 4
c = a;
mov eax, dword ptr [ebp+8]
mov dword ptr [ebp-4], eax
and so on for the other variables.
However, this is what I find when debugging :
push ebp
mov ebp,esp
sub esp,0CCh // normal since it's in debug with ZI option
push ebx
push esi
push edi
lea edi,[ebp-0CCh]
mov ecx,33h
mov eax,0CCCCCCCCh
rep stos dword ptr es:[edi]
size_t c;
c = a;
mov eax,dword ptr [a]
mov dword ptr [c],eax
Why is it using the variable a directly instead of calling the value stored in the stack? I don't understand...
The debugger doesn't show the instruction using ebp to access a. The same syntax is permitted when you write inline assembly. Otherwise the reason that dword ptr still appears.
It is easy to get it your preferred way, right click > untick "Show Symbol Names".
Using the assembly output option (right click on file name, properties, ...), I get what you expect from debug assembly output. This could depend on which version of VS you use. For this example, I used VS2005. I have VS2015 on a different system, but didn't try it yet.
_c$ = -8 ; size = 4
_a$ = 8 ; size = 4
_b$ = 12 ; size = 4
_ex PROC ; COMDAT
push ebp
mov ebp, esp
sub esp, 204 ; 000000ccH
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-204]
mov ecx, 51 ; 00000033H
mov eax, -858993460 ; ccccccccH
rep stosd ;fill with 0cccccccch
mov eax, DWORD PTR _a$[ebp]
mov DWORD PTR _c$[ebp], eax
mov eax, DWORD PTR _b$[ebp]
mov DWORD PTR _a$[ebp], eax
mov eax, DWORD PTR _c$[ebp]
mov DWORD PTR _b$[ebp], eax
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
_ex ENDP
Note this doesn't work, you need to use pointers for the swap to work.
void ex(size_t *pa, size_t *pb)
{
size_t c;
c = *pa;
*pa = *pb;
*pb = c;
}
which gets translated into:
_c$ = -8 ; size = 4
_pa$ = 8 ; size = 4
_pb$ = 12 ; size = 4
_ex PROC ; COMDAT
push ebp
mov ebp, esp
sub esp, 204 ; 000000ccH
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-204]
mov ecx, 51 ; 00000033H
mov eax, -858993460 ; ccccccccH
rep stosd
mov eax, DWORD PTR _pa$[ebp]
mov ecx, DWORD PTR [eax]
mov DWORD PTR _c$[ebp], ecx
mov eax, DWORD PTR _pa$[ebp]
mov ecx, DWORD PTR _pb$[ebp]
mov edx, DWORD PTR [ecx]
mov DWORD PTR [eax], edx
mov eax, DWORD PTR _pb$[ebp]
mov ecx, DWORD PTR _c$[ebp]
mov DWORD PTR [eax], ecx
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
_ex ENDP
I have a school project about compilers and how it differs in assembly code between Intel x86 and ARMv7, but i'm stuck trying to comprehend the assembly for the Intel x86 architecture.
The source code is:
int main()
{
int a=5,b=2;
int result;
result = a % b;
printf("Result of 5 modulo 2 is %i\n", result);
}
Assembly output (gcc masm=Intel)
main:
/*
Intel32-x86 Arhchitecture
Little endian
ebp register -- base pointer
esp register -- stack pointer
*/
push ebp ; ebp register put on stack
mov ebp, esp ; Move data from ebp to esp
and esp, -16 ; Logical AND ??
sub esp, 32 ; Subtraction ??
mov DWORD PTR [esp+20], 5
;5 as 32 bits
;00000101-00000000-00000000-00000000
mov DWORD PTR [esp+24], 2
;2 as 32 bits
;00000010-00000000-00000000-00000000
mov eax, DWORD PTR [esp+20]
mov edx, eax
sar edx, 31
;Shift Arithmetically right - edx med 31.
;00000101-00000000-00000000-00000000 BEFORE
;00000000-00000000-00000000-00000000 AFTER
idiv DWORD PTR [esp+24]
;Signed divide - IDIV r/m32 - EDX:EAX register
;Dividing EDX:EAX on value of esp+24, and save the remainder in edx.
;EDX:EAX 00000000-00000000-00000000-00000000-00000101-00000000-00000000-00000000
mov DWORD PTR [esp+28], edx
mov eax, OFFSET FLAT:.LC0
mov edx, DWORD PTR [esp+28]
mov DWORD PTR [esp+4], edx
mov DWORD PTR [esp], eax
call printf
leave
ret
and esp, -16 ; Logical AND
sub esp, 32 ; Subtraction
What is the purpose of those two instructions?
The purpose is mentioned in the comments:
and esp,-16 ;round esp down to 16 byte boundary
sub esp,32 ;allocate 32 bytes of space for local variables
In case you didn't catch this part about sign extending the dividend:
mov eax, DWORD PTR [esp+20] ; eax = dividend
mov edx, eax ; edx = dividend
sar edx, 31 ; edx = 0 or -1 (the sign extension)
When compiling a C program to an object file, it's easy to get the Microsoft compiler to give you an annotated disassembly (with names of functions and variables, source line numbers etc.) using cl /Fa.
I'm trying to get something similar from the final linked executable (assuming the program was compiled with appropriate debug information), which seems to be trickier; dumpbin and objdump seem to only provide non-annotated disassembly.
What's the best way to obtain this?
if you have the program compiled with debuginfo windbg should provide disassembly of a function with line numbers
sample code compiled with debug info and an assembly file generated with /Fa
C:\codesnips\comparesrc\debug>cl /Zi /Fa comparesrc.cpp /link /Debug
comparesrc.cpp
/out:comparesrc.exe
/debug
/Debug
comparesrc.obj
the source for the above compilation
C:\codesnips\comparesrc\debug>type comparesrc.cpp
#include <stdio.h> // standard include file
int main (void)
{ // this line will become prolog
printf("hello my dear source compare\n"); // see str in .data section
puts("c"); // will put a char* with line break to console
puts("om");
puts("pare");
int a,b,c,d;
a = 2; b =3 ; c = 4;
d = a+b-c; // 2+3 -4 = 1
printf("%d\n",d); // should print 1
d = (a*b)/c; // 2*3 /4 = 6 /4 numerator = 1
printf("%d\n",d); // should printf 1
d = (a*b)%c; // 2 * 3 % 4 denominator = 2
printf("%d\n",d); // should print 2
return 0; // lets generate a cod file and see the assembly
} // this line will get converted to epilog
the assembly file created by /Fa switch
C:\codesnips\comparesrc\debug>type comparesrc.asm
; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01
TITLE C:\codesnips\comparesrc\debug\comparesrc.cpp
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
CONST SEGMENT
$SG3850 DB 'hello my dear source compare', 0aH, 00H
ORG $+2
$SG3851 DB 'c', 00H
ORG $+2
$SG3852 DB 'om', 00H
ORG $+1
$SG3853 DB 'pare', 00H
ORG $+3
$SG3858 DB '%d', 0aH, 00H
$SG3859 DB '%d', 0aH, 00H
$SG3860 DB '%d', 0aH, 00H
CONST ENDS
PUBLIC _main
EXTRN _puts:PROC
EXTRN _printf:PROC
; Function compile flags: /Odtp
_TEXT SEGMENT
_c$ = -16 ; size = 4
_d$ = -12 ; size = 4
_b$ = -8 ; size = 4
_a$ = -4 ; size = 4
_main PROC
; File c:\codesnips\comparesrc\debug\comparesrc.cpp
; Line 3
push ebp
mov ebp, esp
sub esp, 16 ; 00000010H
; Line 4
push OFFSET $SG3850
call _printf
add esp, 4
; Line 5
push OFFSET $SG3851
call _puts
add esp, 4
; Line 6
push OFFSET $SG3852
call _puts
add esp, 4
; Line 7
push OFFSET $SG3853
call _puts
add esp, 4
; Line 9
mov DWORD PTR _a$[ebp], 2
mov DWORD PTR _b$[ebp], 3
mov DWORD PTR _c$[ebp], 4
; Line 10
mov eax, DWORD PTR _a$[ebp]
add eax, DWORD PTR _b$[ebp]
sub eax, DWORD PTR _c$[ebp]
mov DWORD PTR _d$[ebp], eax
; Line 11
mov ecx, DWORD PTR _d$[ebp]
push ecx
push OFFSET $SG3858
call _printf
add esp, 8
; Line 12
mov eax, DWORD PTR _a$[ebp]
imul eax, DWORD PTR _b$[ebp]
cdq
idiv DWORD PTR _c$[ebp]
mov DWORD PTR _d$[ebp], eax
; Line 13
mov edx, DWORD PTR _d$[ebp]
push edx
push OFFSET $SG3859
call _printf
add esp, 8
; Line 14
mov eax, DWORD PTR _a$[ebp]
imul eax, DWORD PTR _b$[ebp]
cdq
idiv DWORD PTR _c$[ebp]
mov DWORD PTR _d$[ebp], edx
; Line 15
mov eax, DWORD PTR _d$[ebp]
push eax
push OFFSET $SG3860
call _printf
add esp, 8
; Line 16
xor eax, eax
; Line 17
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END
and finally disassembly of the complete main function using cdb (console version of windbg)
cdb -c ".lines;g main;uf #eip;q;" comparesrc.exe
Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
CommandLine: comparesrc.exe
0:000> cdb: Reading initial command '.lines;g main;uf #eip;q;'
Line number information will be loaded
comparesrc!main [c:\codesnips\comparesrc\debug\comparesrc.cpp # 3]:
3 00401010 55 push ebp
3 00401011 8bec mov ebp,esp
3 00401013 83ec10 sub esp,10h
4 00401016 685c8c4100 push offset comparesrc!__xt_z+0x120 (00418c5c)
4 0040101b e81b020000 call comparesrc!printf (0040123b)
4 00401020 83c404 add esp,4
5 00401023 687c8c4100 push offset comparesrc!__xt_z+0x140 (00418c7c)
5 00401028 e8bf000000 call comparesrc!puts (004010ec)
5 0040102d 83c404 add esp,4
6 00401030 68808c4100 push offset comparesrc!__xt_z+0x144 (00418c80)
6 00401035 e8b2000000 call comparesrc!puts (004010ec)
6 0040103a 83c404 add esp,4
7 0040103d 68848c4100 push offset comparesrc!__xt_z+0x148 (00418c84)
7 00401042 e8a5000000 call comparesrc!puts (004010ec)
7 00401047 83c404 add esp,4
9 0040104a c745fc02000000 mov dword ptr [ebp-4],2
9 00401051 c745f803000000 mov dword ptr [ebp-8],3
9 00401058 c745f004000000 mov dword ptr [ebp-10h],4
10 0040105f 8b45fc mov eax,dword ptr [ebp-4]
10 00401062 0345f8 add eax,dword ptr [ebp-8]
10 00401065 2b45f0 sub eax,dword ptr [ebp-10h]
10 00401068 8945f4 mov dword ptr [ebp-0Ch],eax
11 0040106b 8b4df4 mov ecx,dword ptr [ebp-0Ch]
11 0040106e 51 push ecx
11 0040106f 688c8c4100 push offset comparesrc!__xt_z+0x150 (00418c8c)
11 00401074 e8c2010000 call comparesrc!printf (0040123b)
11 00401079 83c408 add esp,8
12 0040107c 8b45fc mov eax,dword ptr [ebp-4]
12 0040107f 0faf45f8 imul eax,dword ptr [ebp-8]
12 00401083 99 cdq
12 00401084 f77df0 idiv eax,dword ptr [ebp-10h]
12 00401087 8945f4 mov dword ptr [ebp-0Ch],eax
13 0040108a 8b55f4 mov edx,dword ptr [ebp-0Ch]
13 0040108d 52 push edx
13 0040108e 68908c4100 push offset comparesrc!__xt_z+0x154 (00418c90)
13 00401093 e8a3010000 call comparesrc!printf (0040123b)
13 00401098 83c408 add esp,8
14 0040109b 8b45fc mov eax,dword ptr [ebp-4]
14 0040109e 0faf45f8 imul eax,dword ptr [ebp-8]
14 004010a2 99 cdq
14 004010a3 f77df0 idiv eax,dword ptr [ebp-10h]
14 004010a6 8955f4 mov dword ptr [ebp-0Ch],edx
15 004010a9 8b45f4 mov eax,dword ptr [ebp-0Ch]
15 004010ac 50 push eax
15 004010ad 68948c4100 push offset comparesrc!__xt_z+0x158 (00418c94)
15 004010b2 e884010000 call comparesrc!printf (0040123b)
15 004010b7 83c408 add esp,8
16 004010ba 33c0 xor eax,eax
17 004010bc 8be5 mov esp,ebp
17 004010be 5d pop ebp
17 004010bf c3 ret
You can use
Windbg -z <any image>
to perform disassembly or any inspection of that image (works with cdb \ kd as well).
You can see source lines, symbols, types - without having to actually run the program.
This is useful for looking at DLLs, but really necessary when you want to look at code compiled for another architecture or a device driver where you can't run in on your machine.
For example
cdb -z ntoskrnl.exe
will let you inspect the code of the windows kernel.
This is more powerful than a crashdump because you don't just see the code that is paged in - you can see all the code that is in the .exe
Compiling this simple function with MSVC2008, in Debug mode:
int __cdecl sum(int a, int b)
{
return a + b;
}
I get the following disassembly listing:
int __cdecl sum(int a, int b)
{
004113B0 push ebp
004113B1 mov ebp,esp
004113B3 sub esp,0C0h
004113B9 push ebx
004113BA push esi
004113BB push edi
004113BC lea edi,[ebp-0C0h]
004113C2 mov ecx,30h
004113C7 mov eax,0CCCCCCCCh
004113CC rep stos dword ptr es:[edi]
return a + b;
004113CE mov eax,dword ptr [a]
004113D1 add eax,dword ptr [b]
}
004113D4 pop edi
004113D5 pop esi
004113D6 pop ebx
004113D7 mov esp,ebp
004113D9 pop ebp
004113DA ret
There are some parts of the prolog I don't understand:
004113BC lea edi,[ebp-0C0h]
004113C2 mov ecx,30h
004113C7 mov eax,0CCCCCCCCh
004113CC rep stos dword ptr es:[edi]
Why is this required?
EDIT:
After removing the /RTC compiler option, as was suggested, most of this code indeed went away. What remained is:
int __cdecl sum(int a, int b)
{
00411270 push ebp
00411271 mov ebp,esp
00411273 sub esp,40h
00411276 push ebx
00411277 push esi
00411278 push edi
return a + b;
00411279 mov eax,dword ptr [a]
0041127C add eax,dword ptr [b]
}
Now, why is the: sub esp, 40h needed? It's as if place is being allocated for local variables, though there aren't any. Why is the compiler doing this? Is there another flag involved?
This code is emitted due to the /RTC compile option. It initializes all local variables in your function to a bit pattern that is highly likely to generate an access violation or to cause unusual output values. That helps you find out when you forgot to initialize a variable.
The extra space in the stack frame you see allocated is there to support the Edit + Continue feature. This space will be used when you edit the function while debugging and add more local variables. Change the /ZI option to /Zi to disable it.
and in any case of buffer overflow (if you would overwrite local variables) you will end up in a field of "int 3" opcodes:
int 3 ; 0xCC
int 3 ; 0xCC
int 3 ; 0xCC
int 3 ; 0xCC
int 3 ; 0xCC
int 3 ; 0xCC
...
that can be catched by the debugger, so you can fix your code