asm2: improve example

This commit is contained in:
KoroLion 2020-12-03 15:56:26 +03:00
parent e9923eeba1
commit 8577ab66d8
4 changed files with 42 additions and 25 deletions

View File

@ -1 +1 @@
nasm sum.asm -f elf && gcc main.c sum.o -m32
nasm utils.asm -f elf && gcc main.c utils.o -m32

View File

@ -5,14 +5,14 @@ Copyright 2020 KoroLion (github.com/KoroLion)
#include <stdio.h>
float sum(float a, float b);
float fx(float x);
int main() {
float a, b;
scanf("%f %f", &a, &b);
float c = sum(a, b);
float c = sum(1.5, 2.1);
printf("%f\n", c);
float y = fx(2);
printf("%f\n", y);
return 0;
}

View File

@ -1,19 +0,0 @@
; Copyright 2020 KoroLion (github.com/KoroLion)
global sum
section .data
section .bss
temp: resb 4
section .text
; sum(x, y)
sum:
finit
fld dword [esp + 4] ; = x
fadd dword [esp + 8] ; += y
fst dword [temp] ; [temp] = st0
mov eax, [temp]
ret

View File

@ -0,0 +1,36 @@
; Copyright 2020 KoroLion (github.com/KoroLion)
global sum
global fx
section .data
section .bss
temp: resb 4
section .text
; float sum(float x, float y)
; return x + y
sum:
finit
fld dword [esp + 4] ; st0 = x
fadd dword [esp + 8] ; st0 += y
ret ; returns st0 to c
; float fx(float x)
; return 1 / (x + 2)
fx:
; st1
fld dword [esp + 4] ; st1 = x
mov dword [temp], 2
fiadd dword [temp] ; st1 += 2
; st0
mov dword [temp], 1
fild dword [temp] ; st0 = 1
fdiv st0, st1 ; st0 /= st1
ret ; returns st0 to c