Integrating with ml64.exe

I’m back to Windows.
First, I started with writing simple functions in 32 and 64bit assembly. Visual Studio compiler supports 32bit inline assembly, but doesn’t support 64bit. Today, I used ml.exe/ml64.exe for both.

Here is the caller in C.

// asm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#ifdef __cplusplus
extern "C" {
#endif
  char* _message();
  int _add(int a, int b);
  int _sub(int a, int b);
  int _fib(int a);
#ifdef __cplusplus
}
#endif

int _tmain(int argc, _TCHAR* argv[])
{
  int a = 10;
  int b = 20;

  printf("* started\n");
  int result = _add(a, b);

  printf("%d+%d=%d\n", a, b, result);
  result = _sub(a, b);
  printf("%d-%d=%d\n", a, b, result);

  for (int i = 0; i < 20; i++){
    result = _fib(i);
    printf("fib(%d)=%d\n", i, result);
  }
  printf("%s\n", _message());

  printf("* done\n");
  return 0;
}

That calls these.
asm64.asm:

.data
MSG	DB  "Hoge x64,", 0dh, 0ah, "Page!", 0dh, 0ah, 0

.code
; char* _message()
_message PROC
	mov rax, offset MSG
	ret
_message ENDP

; int _add(int a, int b)
_add PROC
add rcx, rdx			
mov rax, rcx
ret
_add ENDP

; int _sub(int a, int b)
_sub PROC
sub rcx, rdx			
mov rax, rcx
ret
_sub ENDP

; int _fib(int a)
_fib PROC
	cmp rcx, 0
	jz @ret0
	cmp rcx, 1
	jz @ret1

	push r12
	push r13

	mov r12, rcx
	sub r12, 1
	mov rcx, r12
	call _fib
	mov r13, rax

	sub r12, 1h
	mov rcx, r12
	call _fib
	add r13, rax

	mov rax, r13
	pop r13
	pop r12
	ret
@ret0:
	mov rax, 0
ret
@ret1:
	mov rax, 1
ret
_fib ENDP

END

asm32.asm:

.data
MSG	DB  "Hoge x64,", 0dh, 0ah, "Page!", 0dh, 0ah, 0

.code
; char* _message()
_message PROC
	mov rax, offset MSG
	ret
_message ENDP

; int _add(int a, int b)
_add PROC
add rcx, rdx			
mov rax, rcx
ret
_add ENDP

; int _sub(int a, int b)
_sub PROC
sub rcx, rdx			
mov rax, rcx
ret
_sub ENDP

; int _fib(int a)
_fib PROC
	cmp rcx, 0
	jz @ret0
	cmp rcx, 1
	jz @ret1

	push r12
	push r13

	mov r12, rcx
	sub r12, 1
	mov rcx, r12
	call _fib
	mov r13, rax

	sub r12, 1h
	mov rcx, r12
	call _fib
	add r13, rax

	mov rax, r13
	pop r13
	pop r12
	ret
@ret0:
	mov rax, 0
ret
@ret1:
	mov rax, 1
ret
_fib ENDP

END

Configure vcproj to run ml/ml64 using "custom build as below".
asm64_config

64 bit result:

C:\Users\sokoide\Projects\Spike\x64\Debug\asm.exe
* started
10+20=30
10-20=-10
fib(0)=0
fib(1)=1
fib(2)=1
fib(3)=2
fib(4)=3
fib(5)=5
fib(6)=8
fib(7)=13
fib(8)=21
fib(9)=34
fib(10)=55
fib(11)=89
fib(12)=144
fib(13)=233
fib(14)=377
fib(15)=610
fib(16)=987
fib(17)=1597
fib(18)=2584
fib(19)=4181
Hoge x64,
Page!

* done

32 bit result:

>C:\Users\sokoide\Projects\Spike\Debug\asm.exe
* started
10+20=30
10-20=-10
fib(0)=0
fib(1)=1
fib(2)=1
fib(3)=2
fib(4)=3
fib(5)=5
fib(6)=8
fib(7)=13
fib(8)=21
fib(9)=34
fib(10)=55
fib(11)=89
fib(12)=144
fib(13)=233
fib(14)=377
fib(15)=610
fib(16)=987
fib(17)=1597
fib(18)=2584
fib(19)=4181
Hoge x86,
Page!

* done

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *