Author Topic: Nasm and C??  (Read 41869 times)

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Nasm and C??
« Reply #15 on: May 30, 2011, 02:56:56 AM »

hmmm....documentation....that's probably the ONE key item missing from the package.  I've been busy trying to finish the full 64-bit Linux/Windows build ( rather tricky, but almost there! ) and should seriously consider adding some docs to the project.  After I get the next RC out ( and I don't hear anything negative ) I'll try and whip up something.  In the meantime you can peruse some of my postings both here and on asmcommunity.net where you'll catch a glimmer of ideas.  ;D

Offline brethren

  • Jr. Member
  • *
  • Posts: 28
Re: Nasm and C??
« Reply #16 on: June 04, 2011, 06:16:30 PM »
i finally got C types working, thanks for the info Rob. Plus dynamic inclusion is working great, thanks Bryant

heres an example
Code: [Select]
%include "stdio.inc"
%include "stdlib.inc"
%include "time.inc"

SECTION .bss

        var1 reserve(time_t) 1

SECTION .text
cproc main

        ccall time, var1
        ccall printf, `Number of seconds since the epoch: %d\n`, time_t[var1]
        
        mov eax, EXIT_SUCCESS

cendproc

« Last Edit: June 04, 2011, 06:18:16 PM by brethren »

Offline brethren

  • Jr. Member
  • *
  • Posts: 28
Re: Nasm and C??
« Reply #17 on: June 05, 2011, 10:16:04 PM »
heres another question  ;D

can you use a procedure that has the same name as an nasm keyword?

i'm just reading about a linux function called 'times' that returns the amount of CPU time a process has used in clock ticks (would be useful for timing code). obviously times is also an nasm directive :(

is it possible to use this function?

btw for anybody using linux the function prototype is in /usr/include/sys/times.h

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Nasm and C??
« Reply #18 on: June 05, 2011, 11:06:29 PM »
can you use a procedure that has the same name as an nasm keyword?

For TIMES in particular, no, as it is handled at assemble time, so I don't think any amount of preprocessing or overloading voodoo will help.

This is where you'd want to prepend an underscore and treat it as _TIMES within NASM and during link phase.

However, and in hindsight, there should be some code within NASM to handle this corner case, e.g. "when TIMES is used as a MEMREF, and if EXTERN or LABEL, then don't ASSEMBLE as TIMES" or something like that.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm and C??
« Reply #19 on: June 05, 2011, 11:15:02 PM »
"$times" for the external function?

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm and C??
« Reply #20 on: June 05, 2011, 11:30:54 PM »
I knew I'd seen it documented! Hard to find, though. There it is, in section 3.1!

"An identifier may also be prefixed with a $ to indicate that it is intended to be read as an identifier and not a reserved word; thus, if some other module you are linking with defines a symbol called eax, you can refer to $eax in NASM code to distinguish the symbol from the register."

Best,
Frank


Offline brethren

  • Jr. Member
  • *
  • Posts: 28
Re: Nasm and C??
« Reply #21 on: June 05, 2011, 11:38:32 PM »
just tested and then disassembled the code, worked perfectly:)
Code: [Select]
%include "sys/times.inc"
SECTION .bss

        buf resb tms_size

SECTION .text
cproc main
        ccall $times, buf
cendproc

Code: [Select]
    0x080483d0 <main+0>:    push   ebp
    0x080483d1 <main+1>:    mov    ebp,esp
    0x080483d3 <main+3>:    push   0x80495c4
    0x080483d8 <main+8>:    call   0x80482fc <times@plt>
    0x080483dd <main+13>:   add    esp,0x4
    0x080483e3 <main+19>:   mov    esp,ebp
    0x080483e5 <main+21>:   pop    ebp
    0x080483e6 <main+22>:   ret   
   
End of assembler dump.
« Last Edit: June 05, 2011, 11:43:43 PM by brethren »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Nasm and C??
« Reply #22 on: June 06, 2011, 12:43:13 AM »
I knew I'd seen it documented! Hard to find, though. There it is, in section 3.1!

Nice find, I'll have to remember that one.