Author Topic: Interface C with basic ASM 64 bit arithmetic  (Read 8986 times)

Offline jordi

  • Jr. Member
  • *
  • Posts: 6
Interface C with basic ASM 64 bit arithmetic
« on: November 06, 2016, 07:08:57 PM »
Hi,

I have written some simple arithmetic routines in 64 bit assembly that work with what C names long type; i.e., 64 bit signed integers (Linux platform).
I attach 2 files:

- asm64_long.asm
the routines in 64 bit assembly

- test_asm64_long.c
a C program to test the routines against their C counterparts

By itself, the routines are too simple to be of any use in a big project, but show the way they can be interfaced in C. Take into account that in Windows the way arguments are passed is different. (See NASM documentation).

To compile the files without the Makefile:

$ nasm -f elf64 asm64_long.asm
$ gcc -Wall test_asm64_long.c asm64_long.o -o test_asm64_long

Feedback is welcome!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Interface C with basic ASM 64 bit arithmetic
« Reply #1 on: November 07, 2016, 06:22:40 PM »
Hi Jordi!

Really good to see you able to post here! I have downloaded your files. I seem to have been the only one! They "look right" to me, but I am not able to test 64-bit right now. "Soon", perhaps. Do they seem to work properly for you?

I hope we can get you some feedback soon. This Forum has a bunch of members, but nobody has much to say. Been like this for as long as I can remember. Nasm users are just not a talkative bunch, I guess. Thanks for posting and I hope you post more - even if you have to talk to yourself!

Best,
Frank


Offline jordi

  • Jr. Member
  • *
  • Posts: 6
Re: Interface C with basic ASM 64 bit arithmetic
« Reply #2 on: November 07, 2016, 09:47:50 PM »

Hi Frank,

Yes, it seems to work. Here is a sample output:

$  ./test_asm64_long 2309865 -4320000076

Addition:
C:     2309865 plus -4320000076 = -4317690211
ASM64: 2309865 plus -4320000076 = -4317690211

Substraction:
C:     2309865 minus -4320000076 = 4322309941
ASM64: 2309865 minus -4320000076 = 4322309941

Increment:
C:     inc(2309865) = 2309866
ASM64: inc(2309865) = 2309866
C:     inc(-4320000076) = -4320000075
ASM64: inc(-4320000076) = -4320000075

Decrement:
C:     dec(2309865) = 2309864
ASM64: dec(2309865) = 2309864
C:     dec(-4320000076) = -4320000077
ASM64: dec(-4320000076) = -4320000077

Sign change:
C:     minus(2309865) = -2309865
ASM64: minus(2309865) = -2309865
C:     minus(-4320000076) = 4320000076
ASM64: minus(-4320000076) = 4320000076

Maximum:
C    : max(2309865, -4320000076) = 2309865
ASM64: max(2309865, -4320000076) = 2309865

Minimum:
C    : min(2309865, -4320000076) = -4320000076
ASM64: min(2309865, -4320000076) = -4320000076

Multiplication:
C:     2309865 times -4320000076 = -9978616975549740
ASM64: 2309865 times -4320000076 = -9978616975549740

Division: quotient
C:     2309865 div -4320000076 = 0
ASM64: 2309865 div -4320000076 = 0

Division: remainder
C:     2309865 mod -4320000076 = 2309865
ASM64: 2309865 mod -4320000076 = 2309865

Dision: quotient and remainder in one:
ASM64: 2309865 mdiv -4320000076 : (q: 0 r: 2309865)

Now I see a little bug: Dision for Division  ;)

Best,
Jordi

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Interface C with basic ASM 64 bit arithmetic
« Reply #3 on: November 07, 2016, 09:58:26 PM »
Ah. Thought it looked alright. Missed "Dision". Thanks for the feedback, Jordi!

Best,
Frank