Files
assembly/x86_64/2_homework.asm
2019-03-13 19:54:05 +01:00

51 lines
1.1 KiB
NASM

global _start ; entry point label
section .data
str: db "Please give me a string: " ; \n = 0xA
strSize: equ $ - str
stdin: equ 0 ; POSIX
stdout: equ 1 ; POSIX
section .bss
strBuffer: resb 64 ; reserve 64 bytes
strBufferSize: equ 64 ; same as above
section .text
; Syscall clobbered registers
; rax rdi rsi rdx r10 r8 r9
_start:
mov rax, 1
mov rdi, stdout ; fd to write to
mov rsi, str ; pointer
mov rdx, strSize ; size
syscall
xor rax, rax ; 0
mov rdi, stdin ; fd to read from
mov rsi, strBuffer ; where to store it
mov rdx, strBufferSize ; maximum number of bytes
syscall
mov rbx, rax ; store the read bytes
; Calculate the halfway point of read bytes
dec rax ; subtract the read newline from it
xor rdx, rdx ; clear high bits dividend
; rax is already set
mov rdi, 2 ; set the divisor
div rdi ; rax = rax / rdi
; Alter the middle byte
mov rdx, strBuffer
add rdx, rax
mov byte [rdx], '_'
mov rax, 1
mov rdi, stdout ; fd to write to
mov rsi, strBuffer ; pointer
mov rdx, rbx ; size
syscall
mov rax, 60
xor rdi, rdi
syscall