开发者

Call C function from Assembly code using as88 Assembler

I'm working on a Floating Point calculator for 16bits processors, specifically 8086/8088. I'm using as88 Tracker which doesn't implement floating points, not allowing me to use sscanf with "%f".

I thought about doing that in C code and calling this function from my Assembly code but couldn't find out how to do it.

This is my code so far:

 #include "../syscalnr.h"

.sect .text
_code_:     
        push    bp
        mov bp, sp

        push    SEGOP-PRIOP ! Pushes PRIOP String Size into the stack
        push    PRIOP       
        push    STDOUT      
        push    _WRITE          ! System Call to print string on the display
        sys

        a开发者_高级运维dd sp, 8
        mov di, rasc    ! Prepares DI to receive char
        push    _GETCHAR
1:      sys
        cmpb    al, '\n'    ! Compares with EOL and keeps storing the string chars
        je  2f
        stosb           ! Stores char into variable rasc
        jmp 1b

2:      xorb    al, al      ! Clears registers
        add sp, 2

.sect .data
_data_:

PRIOP:      .asciz  "Insert first operand:\n "
SEGOP:      .ascii  "Insert second operand: "

FORMAT:     .asciz  "%u"
F_CHAR:     .asciz  "%c"
F_STR:      .asciz  "%s\n"

.sect .bss
_bss_:          
rasc:       .space  10

I want to be able to write a C function as:

float* getVal(char* ch) {

    float fVal;
    sscanf(ch, "%f", &fVal);

    if(fVal == 0) return 0;

    return fVal;
}

And call it from my Assembly code to translate the string number input by the user into a float.

Can anyone help me with that?

Thanks!


There is a C-language function calling convention that dictates how registers need to be set up when the function is entered. You'll have to find out what that convention is -- maybe by looking at a C-compiled obj file -- and then make your asm code adhere to it just like you do when you execute sys . I don't think that will be your only problem, though, because sscanf( ) undoubtedly calls tons of other functions in the C library which you'll then need to find; and understand; and integrate; and debug; and on and on. Forget that noise.

Unsolicited-advice alert: It might be easier -- it would be easier if I were doing it -- to just parse the input right there in your asm code. If the guy is typing '3.14159' that's pretty easy, isn't it? Even if you're seeing input in scientific notation, that's not too bad, imo.


First at all, this will only work if your C library is running on the emulator too. I don't know exactly how close your 8088 emulator adheres to the x86 ABI, but assuming it hasn't changed much in the last 20 years (cough) you call C functions from assembler like that:

    push    RETVAL    ;last parameter first (address of float to return)
    push    STRFLOAT  ;first parameter last (format string)
    call    sscanf    ;error code is in ax
    add     sp,4      ;returned float is at RETVAL
    ;do something...

.sect .data
    STRFLOAT: .asciz  "%f"

.sect .bss
    RETVAL:   .space 4

Depending on the name mangling you may need to replace sscanf with _sscanf or sscanf_.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜