Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Assembly Language > Calling asm fro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 4900 of 5181
Post > Topic >>

Calling asm from C

by Frank Kotler <fbkotler@[EMAIL PROTECTED] > Mar 8, 2008 at 02:32 AM

Since this newsgroup is *determined* to talk about C recently, the 
*least* we can do is call some asm from it!

Us "purists" don't like it much, but in the real world, most asm is in 
subroutines called from C[++]. The hard parts are done in asm, and C[++] 
provides the "glue" that holds it together, and provides the OS-specific 
parts. (not going to be ****table across CPUs, of course)

An alternative to this would be inline asm. When the C folks talk about 
****tability, they ain't talking about cross-toolchain ****tability of 
inline asm!!! Awful mess. Maybe gcc on Linux (BSD/Mac) vs gcc on Windows 
would work...

An object file of the proper format will (should) link with object 
file(s) of the same format, regardless of *what* language the were 
written in ("Python" is popular... I wanna wrestle a carniverous reptile 
that outweighs me... Yeah, right!). Different kind of "****tability".

Since Nasm will produce object files in a variety of formats, it's a 
suitable tool for the asm part. We won't have "binary ****tability" of 
course, it'll have to be re-assembled for each OS we want to use it with 
(same as C)...

I don't suppose C has a "getcpuvendor()"... maybe it does. Struck me as 
something this might be "good for". (a better example might pass more 
parameters...) Something like:

bits 32
global getvendor

section .text
getvendor:
     pusha
     xor eax, eax
     cpuid
     mov eax, [esp + 36]
     mov [eax], ebx
     mov [eax + 4], edx
     mov [eax + 8], ecx
     mov byte [eax + 12], 0
     popa
     xor eax, eax
     ret

For Linux, we can assemble this as "nasm -f elf getvendor.asm" - add 
"-g" or "-O" switches if you want... For other-than-ELF, we probably 
want an underscore on "getvendor", so "nasm -f win32 --prefix _ 
getvendor.asm". I'm not sure if MacIntel wants underscores for "-f 
macho" or not. If my information is correct, Watcom C wants a *trailing* 
underscore... "--postfix _" should do it. Hmmmm, what output format 
would Watcom want? Hmmm... Borland wants "-f obj", I believe. "-f obj" 
defaults to "bits 16", so we'll need "bits 32" if we want it to work 
with "-f obj" - won't hurt "-f elf", etc... There, I put it in 
(improving already!).

Now, we'll want something to call it from...

main()
  {char vendorbuf[13];
   getvendor(vendorbuf);

   puts(vendorbuf);
  }

gcc -o getvendortest getvendortest.c getvendor.o

That's horribly improper C. Won't tolerate "-Wall". We'll want "int 
main"... "puts" is implicitly declared in "stdio.h", I guess. We could 
include a "getvendor.h"... hardly seems worthwhile. "Prototype" it here? 
(before or after "main"...?) An explict "return". What's the "minimal" 
but "correct" way to do it?

I don't know how to call it from VB or whateverall... any gurus? Hey, we 
can call it from asm!

global _start
extern getvendor

section .text
_start:
     nop
commence:
     push vendorbuf
     call getvendor
     add esp, 4

     mov ecx, vendorbuf
     mov edx, 12
     call write_stdout

     mov ebx, eax
     mov eax, 1
     int 80h

;------------------
write_stdout:
     mov ebx, 1			; STDOUT
     mov eax, 4			; __NR_write
     int 80h
     ret
;-------------------

section .bss
     vendorbuf resb 13
;-----------------------

That'll have to be changed for other-than-Linux, of course. WriteFile... 
put it in a window with some fancy font, if you like... Maybe, for 
Windows, we ought to arrange for "ret 4" in the callee so it'd be more 
like an API call?

Just a first draft...

Best,
Frank
 




 7 Posts in Topic:
Calling asm from C
Frank Kotler <fbkotler  2008-03-08 02:32:32 
Re: Calling asm from C
santosh <santosh.k83@[  2008-03-08 12:59:10 
Re: Calling asm from C
Frank Kotler <fbkotler  2008-03-08 19:35:52 
Re: Calling asm from C
santosh <santosh.k83@[  2008-03-09 01:43:17 
Re: Calling asm from C
Frank Kotler <fbkotler  2008-03-08 22:18:50 
Re: Calling asm from C
"Rod Pemberton"  2008-03-08 03:02:15 
Re: Calling asm from C
Frank Kotler <fbkotler  2008-03-08 19:42:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 11:34:21 CST 2008.