i have the following code in assembly:
.686
.model flat, stdcall
.stack 4096
ExitProcess proto, dwExitCode : dword
.data
Temporal DWORD ?
mdc_result DWORD ?
.code
main PROC
push ebp
mov ebp, esp
push 120
push 25
call MCD_iterativo
pop ebp
ret 0
main ENDP
MCD_iterativo PROC ; a = 4, b = 8
push ebp
mov ebp, esp
jmp L2
pop ebp
ret
MCD_iterativo ENDP
L2:
cmp DWORD PTR [ebp + 8], 0
je L1
mov eax, DWORD PTR [ebp + 8]
mov DWORD PTR Temporal, eax
mov eax, DWORD PTR [ebp + 12]
cdq
idiv DWORD PTR [ebp + 8]
mov DWORD PTR [ebp + 8], edx
mov ecx, DWORD PTR Temporal
mov DWORD PTR [ebp + 12], ecx
jmp L2
L1:
mov eax, DWORD PTR [ebp + 12]
pop ebp
ret 0
END main
I'm getting the following error: Exception occurred at 0x00000005 in ASM_Miercoles_Semana10.exe: 0xC0000005: Access violation when executing location 0x00000005.
I tried making an add
into the esp and ebp variable to recover the initial address but it doesnt know, i just want to finish the program without errors
I'm trying to implement two functions and the second function have a while inside of it, the loop is working well, i have the error at the end of all, in the ret of main PROC
call MCD_iterativodoesn't pop the args you pushed, so you eventuallyretand pop125into EIP. IDK why you'd be getting an error at0x00000005instead of 0x7d (125 in hex). Oh, the code insideMCD_iterativo/L2modifies its stack arg (which is fine), so you're returning to GCD(5,120) = 5. No need to store/reload anything in that function; looks like un-optimized compiler output..model flat, stdcallmight make MASM magically turnret 0intoret 8, but maybe an explicitret 0overrides that. And you didn't tell MASM about the args the function takes, just a bareproc, which is fine it just means that MASM can't magically change it into a stdcall function that pops the stack as it returns.ret 0, there is a bigger issue. MASM doesn't know how many parametersMCD_iterativo PROChas because no parameters have been specified (so doesn't know it should beret 8). Example:MCD_iterativo PROC arga:DWORD, argb:DWORD. That would changerettoret 8. That only applies to the code inside thePROC. He jumps toL2outside thePROCso thatretwill not be changed toret 8. As well once you specify at least one argument toMCD_iterativothere is no need to do the stack prologue and epilogue since they would be done for you.L2outside the PROC might have been an attempt to fix the stack issue but that method failed.stdcallfor you, you could add it to the duplicate list. Good point about needing to remove the manual EBP setup if MASM does it for you, otherwise the offset would be wrong.