97.5k views
3 votes
You should all remember the distance formula from high school geometry/algebra.

Write a NASM (x86-64) program to calcuate this with only macros/routines in the main program. This should make the algorithm more transportable with appropriate library.

//MAIN PROGRAM

GET X1
GET X2
GET Y1
GET Y2
ASSIGN V1,X2
SUB V1,X1
MULT V1,V1
ASSIGN V2,Y2
SUB V2,Y1
MULT V2,V2
ADD V1,v2
SQRT V1
PRINTSTR “THE VALUE IS”
PRINTINT V1

You can call SQRT in the C library.

1 Answer

6 votes

Here's an example NASM (x86-64) program that calculates the distance between two points using the given pseudo-code.

section .data

format db "%lf", 0 ; format specifier for scanf and printf

section .bss

x1 resq 1

x2 resq 1

y1 resq 1

y2 resq 1

v1 resq 1

v2 resq 1

section .text

extern sqrt, printf, scanf

global main

%macro GET 1

mov rdi, format

mov rsi, %1

call scanf

%endmacro

%macro ASSIGN 2

mov %1, %2

%endmacro

%macro SUB 2

mov rax, %1

sub rax, %2

mov %1, rax

%endmacro

%macro MULT 2

mov rax, %1

imul rax, %2

mov %1, rax

%endmacro

%macro ADD 2

mov rax, %1

add rax, %2

mov %1, rax

%endmacro

%macro SQRT 1

movsd xmm0, %1

call sqrt

movsd %1, xmm0

%endmacro

%macro PRINTSTR 1

mov rdi, format

mov rsi, %1

call printf

%endmacro

%macro PRINTINT 1

mov rdi, format

mov rax, %1

call printf

%endmacro

main

User Alireza Daryani
by
8.2k points