flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > High Level Languages > Returning a value from asm to C.

Author
Thread Post new topic Reply to topic
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
Returning a value from asm to C.
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 charfile_name)
{
        asm("push eax");
        // AH = 0x04Zicronix 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 existswell 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
Post 03 Mar 2014, 11:36
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
sid123,

How different are assembly sources (IIRC Smaller C by Alexey A. Frounze produces NASM output) for working and non-working programs?
Post 03 Mar 2014, 11:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15096
Location: The Unicomplex
Why is there no leave and ret after the last line of mov eax,1? Is that done automatically be the compiler?

Is the value of esi okay to corrupt? The push/pop of eax seems rather pointless.
Post 03 Mar 2014, 12:26
View user's profile Send private message Visit poster's website Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
@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     ebpesp
        jmp     L2
L1:
; loc     _ret : (@-4): unsigned
; =
; RPN'ized expression: "0 "
; Expanded expression: "0 "
; Expression value: 0
; Fused expression:    "=(204) *(@-4) 0 "
        mov     eax0
        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     eax0
L3:
        leave
        ret
L2:
        sub     esp4
        jmp     L1

; glb dummy : () unsigned
section .text
        global  _dummy
_dummy:
        push    ebp
        mov     ebpesp
        jmp     L6
L5:
; return
; RPN'ized expression: "7 "
; Expanded expression: "7 "
; Expression value: 7
; Fused expression:    "7 "
        mov     eax7
        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     eax0
        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     eax0
        jne     L53



_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 03 Mar 2014, 12:37
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.