How to resolve the algorithm Sleep step by step in the X86 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sleep step by step in the X86 Assembly programming language

Table of Contents

Problem Statement

Write a program that does the following in this order:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sleep step by step in the X86 Assembly programming language

Source code in the x86 programming language

%macro sysdef 2
  %define sys_%1        %2
%endmacro
sysdef write,           1
sysdef exit,            60
sysdef nanosleep,       35

%macro inv 1-7 0,0,0,0,0,0
  mov r9,%7
  mov r8,%6
  mov r10,%5
  mov rdx,%4
  mov rsi,%3
  mov rdi,%2
  mov rax,sys_%1
  syscall
%endmacro

section .data
  timeval:
    tv_sec   dq 0
    tv_nsec  dq 0

section .rodata
  str1 db "Sleeping",0xa,0
  str2 db "Awake!",0xa,0

section .text

  global main
  main:
    lea rbx, [rel str1]
    inv write, 1, rbx, 9
    mov qword [rel tv_sec], 5
    mov qword [rel tv_nsec], 0
    lea rax, [rel timeval]
    inv nanosleep, rax, 0
    lea rbx, [rel str2]
    inv write, 1, rbx, 7
    lea rbx, [rel str2]
    inv exit, 0
    ret


; x86_64 linux nasm

section .text

Sleep:
  mov rsi, 0 ; we wont use the second sleep arg, pass null to syscall
  sub rsp, 16
  mov qword [rsp], rdi ; number of seconds the caller requested
  mov qword [rsp + 8], rsi ; we won't use the nanoseconds
  mov rdi, rsp ; pass the struct that's on the stack to
  mov rax, 35 ; sys_nanosleep
  syscall
  add rsp, 16 ; clean up stack
  ret

  

You may also check:How to resolve the algorithm Set consolidation step by step in the Wren programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the C programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Rust programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the jq programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the F# programming language