newbie question: calling asm routines from c 
Author Message
 newbie question: calling asm routines from c

Hi Folks,

I have a set of routines in TASM which were written to be callable from
Quickbasic. I want to be able to call them from C. (I'm using Borland C
4.5) I am sure this is trivial but I haven't the faintest idea about
assembly language. I understand that C needs underscores put in front of
assembler routine names in order that they can be called but I haven't yet
had any luck with that yet. I'd really appreciate it if someone could
either explain how this could be done or point me in the direction of some
info that would explain how to do it. I checked the group FAQ but if there
was anything there that explained it I'm afraid it went way over my head.

Here's an example of one the routines that I need to convert:

;       Putchar is a public procedure that checks the status of the
;       transmit buffer, waits for it to go empty if necessary, then
;       sends the char passed to it by the caller.
;       Quickbasic Example:
;       for a=asc("A") to asc("Z")  ' put the ascii values of A-Z in a
;               call putchar(a)         ' print a
;       next a                          ' loop
;       call putchar (10)               ' print a carriage return
;       call putchar (13)               ' print a linefeed

        public  putchar

putchar proc
        push    bp
        mov     bp,sp                   ; get pointer to argument
        mov     bx,[bp+6]               ; offset of char to send
        mov     ax,[bx]                 ; get it in al

        push    ds                      ; need ds to address V-25 I/O
        push    si                      ; need index register

        push    ax                      ; save ax while we load es
        mov     ax,0F000h               ; point to v-25 I/O
        mov     ds,ax                   ; load it into es
        pop     ax

        mov     si,sst1
        mov     ah,[si]                 ; get serial status register
        test    ah,00100000b            ; test bit 5
        jz      tx_buffer_full          ; loop until it goes high

        mov     si,txb1                 ; point to transmit buffer
        mov     [si],al                 ; send the char in al

        pop     si                      ; restore si
        pop     ds                      ; restore ds
        pop     bp                      ; restore bp

        ret     2                       ; return with one parameter

putchar endp

Thanks very much,

Ben Kenward
Ben Kenward                            Address for post:
Department of Zoology                  Wolfson College
Oxford University                      Linton Road
01865 284387                           Oxford OX2 6UD

Fri, 15 Aug 2003 20:40:39 GMT  
 newbie question: calling asm routines from c
In your ASM routine, put in a line like this:

public _putchar

And in your C code, put in a line like this:

extern TYPE putchar;

where "TYPE" equates to putchar's type/prototype:

i.e. extern void putchar(int);

or whatever "type" the function has...and then if you link them together,
they can now "see" each other...of course, make sure that the calling
convention of the ASM routine and what C expects it to be are the same (but
as your ASM code example seems to do all the correct stack stuff then I
assume you know how that's done already ;) or your stack will get messed up
and it will blow up later :)

Oh...and watch out for C++ name mangling...that can confuse the issue
too...either make sure it compiles a C program and not C++ or (better
overall solution) wrap the declaration in an extern "C" for C++, which will
force it to use the C convention and not the weird C++ name-mangling stuff

extern "C"
    void putchar(int);


Umm...think that's about it...probably forgotten something, though, I do
that sometimes...if so, post again and moan at me for forgetting something :)

Beth :)

P.S. wow! someone who's got just as outdated a C++ compiler as
me...although, I use an old-skool compiler for my own reasons (can't stand
all those automated class wizards and stuff that just annoy me...hehehehe ;)
and not only because I'm skint (which is part of the reason too, I must
confess) :)

Department of Zoology at Oxford University? Surely you guys can afford a new
compiler?...just kidding, just ;)

Sat, 16 Aug 2003 02:16:11 GMT  
 [ 2 post ] 

 Relevant Pages 

1. newbie question: calling asm routines from c

2. Calling asm routines from modula2

3. creating asm routine calls

4. calling MessageBox with asm (newbie)

5. calling MessageBox with asm (newbie)

6. Yet another question on system calls(newbie question)

7. Routine call in Routine

8. Interfacing C routine with CVF routine called from VB

9. asm newbie questions....


11. Newbie Question about ASM

12. Newbie ASM Questions...


Powered by phpBB® Forum Software