Author Topic: call my 'C' function  (Read 9645 times)

nobody

  • Guest
call my 'C' function
« on: July 10, 2009, 01:24:40 PM »
Hello everybody,
I'm a new user of NASM, and I'm porting TASM programs.

My problem is to call a C function like this:

void Function_C(short OffsetIn, short NumByte, short OffsetOut)
{
 ...}

from my NASM program:

...
                       ;OFFSETOUT
   %assign  a    OFF_DPR_%1+0040H    ;=0x240
   push  word  a

;NUMBYTES
                    %assign  b    010H
                    push  word  b

:OffsetIN
   %assign  c    0140H
   push  word  c

CALL     LoadDprPmc1
   ADD   ESP,BYTE 6
...

the error is that the passed values 'a' and 'b' are wrong. (strangley 'c' is correct...)

What I'm doing wrong?
Hope somebody can help me out.
Thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: call my 'C' function
« Reply #1 on: July 10, 2009, 02:23:41 PM »
Not surprising that "c" is correct - not much to go wrong. I'm surprised "b" is wrong - not much to go wrong there, either. Not clear to me what you're doing with "a". What's "OFF_DPR%1"? And why 40h?

I would have thought just:

push OffsetOut
push NumByte ; dword? [NumByte] perhaps?
push OffsetIn
call LoadDprPmc1
add esp, byte 6

... "esp"? Is this 16-bit code or 32-bit? In the latter case, you probably don't want "push word"... do you?

Confusing, the way Tasm and Nasm differ in the way they represent "offset" vs "[contents]". I'm not sure which your variables are supposed to be.

Maybe with more information we could be more help (or maybe not).

Best,
Frank

nobody

  • Guest
Re: call my 'C' function
« Reply #2 on: July 12, 2009, 09:27:09 AM »
Thank you Frank,
I admit, the example above can be confusig.
So then consider this:

;Day to be passed
%assign a 21
push word [a]

;Month to be passed
%assign b 12
push word

;Year to be passed
%assign c 2012
push word [c]

call LoadDprPmc1
add sp, byte 6

I'm in a 16 bit code and I just want to pass 3 values to that C function.
Before begin 'pushing', which register I have to save? no one? I have to use BP , save SP ...something else?

Thank you again!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: call my 'C' function
« Reply #3 on: July 12, 2009, 02:35:12 PM »
I'm afraid I'm still pretty confused. What is the purpose of the "%assign"s?

What you've got there is valid code, but doesn't make a lot of sense. You're passing the word at offset 21 into your data segment (or wherever ds is), etc. You probably want to:

push 21
push 12
push 2012
call the_function
add sp, 6
(presumably this returns 0 :)

More likely, you want to use this for more than just that one day... You could declare variables as either initialized:

segment .data
day dw 21
month dw 12
year dw 2012
...

Or declare 'em uninitialized:

section .bss
day resw 1
month resw 1
year resw 1

You don't want to *use* 'em uninitialized, of course...

mov word [day], 21
mov word [month], 12
mov word [year], 2012
...

(Masm/Tasm will "remember" that we declared the variables as "word", so "word" can be left out, although "word ptr" is often used for clarity. Nasm has amnesia, and needs to be told the size) You can alter initialized data, too. (this is *not* what "%assign" does!)

segment .text
push word [day]
push word [month]
push word [year]
call the_function
add sp, 6

A C function will preserve bx, si, di, and bp, so you don't need to save those in the caller. This implies that ax, cx, and dx may be trashed. ax will be the return value, or dx:ax if it returns a 32-bit result. So the caller must preserve these, if you're using them. Your (original) function was declared "void". This doesn't mean that ax contains "no value", of course, (nor that it is preserved) just that it isn't meaningful. "x = my_void_function(a, b, c);" would be an error in C, but asm wouldn't prevent you from using ax, whether it's "sensible" or not.

If I'm still off-base...  post a bit of the Tasm code you're trying to port. In particular, we'd need to see where the variables are declared, as well as where they're used (IMHO, Masm/Tasm syntax is quite ambiguous - "mov ax, something" in Masm/Tasm would probably translate to "mov ax, [something]" in Nasmese... but not always - depends on what "something" is.) If the Tasm code is available online, just post a link to it. I've had a bit of experience translating Masm/Tasm code to Nasm - a while ago, but I think I remember most of it. :)

Best,
Frank