Assembly x64 Intro - Nasm Example

1.   file add.S


.globl my_add
my_add:
  pushq %rbp
  movq  %rsp, %rbp

  addq %rsi, %rdi
  movq %rdi, %rax
  leaveq
  retq


2.    file max.S


.globl  my_max
my_max:
  pushq    %rbp
  movq     %rsp, %rbp
  movq     %rdi, %rax
  cmpq     %rsi, %rax
  cmovlq   %rsi, %rax
  cmpq     %rdx, %rax
  cmovlq   %rdx, %rax
  leaveq
  retq


3.  file tasm.c


#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

int my_add(int x, int y);
int64_t my_max(int64_t, int64_t, int64_t);

int main(int argc, char *argv[])
{
  int x = 7, y = 23, z = 11;
  int64_t result;

  result = my_add(x, y);

  printf("add result: %" PRId64 "\n", result);

  result = my_max(x, y, z);

  printf("max result: %" PRId64 "\n", result);

  // printf("add result: %ld\n", result);

  return 0;
}


4.   Makefile


tasm.x:
        gcc -g -c add.S -o add_asm.o
        gcc -g -c max.S -o max_asm.o
        gcc -g -c tasm.c -o tasm.o -Wall
        gcc max_asm.o add_asm.o tasm.o -o tasm.x


5.   run

tasm.x


6. result


30

23




你可能感兴趣的:(Assembly x64 Intro - Nasm Example)