Advertisement
Guest User

RV06

a guest
Nov 22nd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern printf
  2. extern scanf
  3.  
  4. global main
  5.  
  6.                 SECTION .data
  7. welcome:        db "RV06: ASM_div", 0x0A, 0x00
  8. prompt:         db "Please enter a number: ", 0x00
  9. output:         db "%d", 0x0A, 0x00
  10. information:    db "Using n/2: %d", 0x0A, 0x00
  11. debug_output:   db "Value: %d", 0x0A, 0x00
  12. input_format:   db "%ld", 0x00
  13.  
  14.                 SECTION .bss
  15. user_input:     resq 1
  16. current_number: resq 1
  17.  
  18.                 SECTION .text
  19. main:           mov     rbp, rsp
  20.                 push    rbp
  21.  
  22. show_welcome:   mov     rdi, welcome            ; Show a little welcome message/title  
  23.                 xor     eax, eax                ; Using xor with itself to set whole register to 0
  24.                 call    printf
  25.  
  26. enter_number:   mov     rdi, prompt             ; Prompt the user for a number
  27.                 xor     rax, rax
  28.                 call    printf
  29.                
  30.                 mov     rdi, input_format       ; Read the number into user_input
  31.                 mov     rsi, user_input
  32.                 xor     rax, rax
  33.                 call    scanf
  34.  
  35. divide_by_two:  xor     rdx, rdx                ; Clear RDX (Used in divisor)
  36.                 mov     rax, [user_input]       ; Get the value of user_input into rax
  37.                 mov     rcx, 2                  ; Get the divisor into rcx
  38.                 div     rcx                     ; Divide rax by rcx
  39.                 mov     [user_input], eax       ; Set the value of user_input to the result (stored in rax)
  40.  
  41.                 mov     rdi, information
  42.                 mov     rsi, [user_input]
  43.                 xor     rax, rax
  44.                 call    printf
  45.  
  46. loop_init:      mov     rax, 1
  47.                 mov     [current_number], rax   ; Start at 1, load through RAX
  48.  
  49. loop_start:     xor     rdx, rdx                ; Clear RDX (before division)
  50.                 mov     rax, [current_number]
  51.                 mov     rcx, 47
  52.                 div     rcx                     ; Divide current number by 47, remainder will be in RDX
  53.  
  54.                 cmp     rdx, 0                  ; Is the remainder 0 (evenly divisible by 47)
  55.                 jne     skip_output
  56.  
  57.                 mov     rdi, output             ; Print the current number
  58.                 mov     rsi, [current_number]
  59.                 xor     rax, rax
  60.                 call    printf
  61.  
  62. skip_output:    mov     rax, [current_number]   ; Compare the current_number
  63.                 cmp     rax, [user_input]       ; to the maximum value defined by the user
  64.                 jge     exit                    ; Exit if greater than or equal
  65.  
  66.                 mov     rcx, [current_number]   ; Increment
  67.                 inc     rcx
  68.                 mov     [current_number], rcx
  69.  
  70.                 jmp     loop_start
  71.  
  72. exit:           mov     rax, 60                 ; syscall exit
  73.                 xor     rdi, rdi                ; exit(0), everything is okay
  74.                 syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement