Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern printf
- extern scanf
- global main
- SECTION .data
- welcome: db "RV06: ASM_div", 0x0A, 0x00
- prompt: db "Please enter a number: ", 0x00
- output: db "%d", 0x0A, 0x00
- information: db "Using n/2: %d", 0x0A, 0x00
- debug_output: db "Value: %d", 0x0A, 0x00
- input_format: db "%ld", 0x00
- SECTION .bss
- user_input: resq 1
- current_number: resq 1
- SECTION .text
- main: mov rbp, rsp
- push rbp
- show_welcome: mov rdi, welcome ; Show a little welcome message/title
- xor eax, eax ; Using xor with itself to set whole register to 0
- call printf
- enter_number: mov rdi, prompt ; Prompt the user for a number
- xor rax, rax
- call printf
- mov rdi, input_format ; Read the number into user_input
- mov rsi, user_input
- xor rax, rax
- call scanf
- divide_by_two: xor rdx, rdx ; Clear RDX (Used in divisor)
- mov rax, [user_input] ; Get the value of user_input into rax
- mov rcx, 2 ; Get the divisor into rcx
- div rcx ; Divide rax by rcx
- mov [user_input], eax ; Set the value of user_input to the result (stored in rax)
- mov rdi, information
- mov rsi, [user_input]
- xor rax, rax
- call printf
- loop_init: mov rax, 1
- mov [current_number], rax ; Start at 1, load through RAX
- loop_start: xor rdx, rdx ; Clear RDX (before division)
- mov rax, [current_number]
- mov rcx, 47
- div rcx ; Divide current number by 47, remainder will be in RDX
- cmp rdx, 0 ; Is the remainder 0 (evenly divisible by 47)
- jne skip_output
- mov rdi, output ; Print the current number
- mov rsi, [current_number]
- xor rax, rax
- call printf
- skip_output: mov rax, [current_number] ; Compare the current_number
- cmp rax, [user_input] ; to the maximum value defined by the user
- jge exit ; Exit if greater than or equal
- mov rcx, [current_number] ; Increment
- inc rcx
- mov [current_number], rcx
- jmp loop_start
- exit: mov rax, 60 ; syscall exit
- xor rdi, rdi ; exit(0), everything is okay
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement