Project

Basic information

Integer registers

Floating-point registers

General remarks

Compiling

Debugging

Examples

Hello world

bits    64
default rel

global  main

extern  printf

section .data
    format      db 'Hello world!', 0xA, 0

section .bss

section .text
    main:
        sub     rsp, 8

        lea     rdi, [format]
        mov     al, 0
        call    printf wrt ..plt

        add     rsp, 8
        sub     rax, rax
        ret

Reading integers

bits    64
default rel
global  main
extern  scanf

section .data
    format_3x_d     db '%d %d %d', 0

section .bss
    array_int       resd 3

section .text
    main:
        sub     rsp, 8

        lea     rcx, [array_int + 8]    ; 4th integer/pointer argument
        lea     rdx, [array_int + 4]    ; 3rd integer/pointer argument
        lea     rsi, [array_int + 0]    ; 2nd integer/pointer argument
        lea     rdi, [format_3x_d]      ; 1st integer/pointer argument
        mov     al, 0                   ; no floating-point arguments
        call    scanf wrt ..plt

        add     rsp, 8
        sub     rax, rax
        ret

Reading floating-point numbers

bits    64
default rel
global  main
extern  scanf

section .data
    format_3x_lf    db '%lf %lf %lf', 0

section .bss
    array_double    resq 3

section .text
    main:
        sub     rsp, 8

        lea     rcx, [array_double + 16]    ; 4th integer/pointer argument
        lea     rdx, [array_double + 8]     ; 3rd integer/pointer argument
        lea     rsi, [array_double + 0]     ; 2nd integer/pointer argument
        lea     rdi, [format_3x_lf]         ; 1st integer/pointer argument
        mov     al, 0                       ; no floating-point arguments
        call    scanf wrt ..plt

        add     rsp, 8
        sub     rax, rax
        ret

Writing floating-point numbers

bits    64
default rel
global  main
extern  printf

section .data
    format_3x_f     db '%f %f %f', 0xA, 0
    align 16
    data            dq 1.0, 2.0, 3.0

section .text
    main:
        sub     rsp, 8

        movlpd  xmm2, [data + 16]       ; 3rd floating-point argument
        movlpd  xmm1, [data + 8]        ; 2nd floating-point argument
        movlpd  xmm0, [data]            ; 1st floating-point argument
        lea     rdi, [format_3x_f]      ; 1st integer/pointer argument
        mov     al, 3                   ; three floating-point arguments
        call    printf wrt ..plt

        add     rsp, 8
        sub     rax, rax
        ret

Comparing floating point numbers

bits    64

default rel

global  main

extern  scanf
extern  printf

section .data
    format_2x_lf            db '%lf %lf', 0
    less_than_str           db '%lf is less than %lf', 0xA, 0
    greater_or_equal_str    db '%lf is greater or equal to %lf', 0xA, 0

section .bss
    array_double    resq 2

section .text
    main:
        sub     rsp, 8

        lea     rdx, [array_double + 8]     ; 3rd integer/pointer argument
        lea     rsi, [array_double + 0]     ; 2nd integer/pointer argument
        lea     rdi, [format_2x_lf]         ; 1st integer/pointer argument
        mov     al, 0                       ; no floating-point arguments
        call    scanf wrt ..plt

        movlpd  xmm0, [array_double]        ; load first value to lower half of xmm0

        movlpd  xmm1, [array_double + 8]    ; load second value to lower half of xmm1
        cmpltsd xmm0, xmm1                  ; check if lower half of xmm0 is LESS-THAN lower half of xmm1
        movq    rax, xmm0                   ; copy the comparison result to RAX
        cmp     rax, 0
        jz      greater_or_equal

    less_than:
        lea     rdi, [less_than_str]
        jmp     print_message

    greater_or_equal:
        lea     rdi, [greater_or_equal_str]

    print_message:
        movlpd  xmm1, [array_double + 8]
        movlpd  xmm0, [array_double]
        mov     al, 2
        call    printf wrt ..plt

        add     rsp, 8
        sub     rax, rax
        ret

Project specification

Requirement for 3.0

Requirement for 3.5

Requirement for 4.0

Requirement for 4.5

Requirement for 5.0