flat assembler
Message board for the users of flat assembler.
Index
> High Level Languages > Returning a value from asm to C. |
Author |
|
sid123 03 Mar 2014, 11:36
Hello,
I have a function called "file_query" which checks whether a file exists or NOT. I use SmallerC to compile my application that runs on the kernel. The function looks like this: Code: /* Zicronix File Query */ unsigned int file_query(unsigned char* file_name) { asm("push eax"); // AH = 0x04, Zicronix File Query asm("mov ah, 0x04"); asm("mov esi, [ebp + 8]"); asm("int 0x50"); asm("pop eax"); // No carry - File Found asm("jnc .ok"); asm("jmp .error"); asm(".error: "); asm("mov eax, 0"); asm("leave"); asm("ret"); asm(".ok: "); // Return value in EAX - one on success asm("mov eax, 1"); } Well, it should return the value on 0 if fail, and 1 on success, but when I try this: Code: unsigned int file_exist_flag = 0; // Query the Kernel for the File Name file_exist_flag = file_query("FILENAME.TXT"); // If it exists, well the file_exist_flag should // be 1. if (file_exist_flag == 0) { printf("Error Finding FILENAME.TXT\n"); kernel_return(); } It returns 0 both ways? However it works when I call it from pure Assembly? Suggestions? -sid123 _________________ "Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X XD |
|||
03 Mar 2014, 11:36 |
|
baldr 03 Mar 2014, 11:59
sid123,
How different are assembly sources (IIRC Smaller C by Alexey A. Frounze produces NASM output) for working and non-working programs? |
|||
03 Mar 2014, 11:59 |
|
sid123 03 Mar 2014, 12:37
@revolution:
Yes "leave, ret" is added by the compiler. ESI seems okay to mess with, although the convention says to preserve ESI, but I have done this on some functions (like printf), and it works ok for now. @baldr: I tried Compiling this: Code: int main(void) { unsigned int _ret = 0; _ret = dummy(); } unsigned int dummy() { return 1; } Returns this: Code: _main: push ebp mov ebp, esp jmp L2 L1: ; loc _ret : (@-4): unsigned ; = ; RPN'ized expression: "0 " ; Expanded expression: "0 " ; Expression value: 0 ; Fused expression: "=(204) *(@-4) 0 " mov eax, 0 mov [ebp-4], eax ; RPN'ized expression: "_ret ( dummy ) = " ; Expanded expression: "(@-4) dummy ()0 =(4) " ; Fused expression: "( dummy )0 =(204) *(@-4) ax " call _dummy mov [ebp-4], eax ; Fused expression: "0 " mov eax, 0 L3: leave ret L2: sub esp, 4 jmp L1 ; glb dummy : () unsigned section .text global _dummy _dummy: push ebp mov ebp, esp jmp L6 L5: ; return ; RPN'ized expression: "7 " ; Expanded expression: "7 " ; Expression value: 7 ; Fused expression: "7 " mov eax, 7 jmp L7 L7: leave ret L6: jmp L5 The original one looks like this; Code: ; Fused expression: "( L49 , printf )4 " push L49 call _printf sub esp, -4 ; loc file_exist_flag : (@-4): unsigned ; = ; RPN'ized expression: "0 " ; Expanded expression: "0 " ; Expression value: 0 ; Fused expression: "=(204) *(@-4) 0 " mov eax, 0 mov [ebp-4], eax ; RPN'ized expression: "file_exist_flag ( L51 file_query ) = " ; Expanded expression: "(@-4) L51 file_query ()4 =(4) " section .data L51: db "FILENAME.TXT",0 section .text ; Fused expression: "( L51 , file_query )4 =(204) *(@-4) ax " push L51 call _file_query sub esp, -4 mov [ebp-4], eax ; if ; RPN'ized expression: "file_exist_flag 0 == " ; Expanded expression: "(@-4) *(4) 0 == " ; Fused expression: "== *(@-4) 0 IF! " mov eax, [ebp-4] cmp eax, 0 jne L53 _________________ "Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X XD |
|||
03 Mar 2014, 12:37 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.