Count the Lines
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 29 of 33.
Challenge
EasyKeep the character count in the loop: the last step prints it again. Also count the newline bytes, and add 1 when the input is not empty and its last byte is not a newline (that last line has no newline after it). Print the number of lines instead of the characters, then a newline.
Example: for the input one, two and three on three lines, the program prints 3.
Write the whole program in main.asm. The locked lib.asm holds print_number and print_char.
Try it yourself
bits 64
extern print_number
extern print_char
section .bss
buffer resb 4096
section .text
global _start
_start:
mov rax, 0
mov rdi, 0
mov rsi, buffer
mov rdx, 4096
syscall
mov rbx, rax
xor r12, r12
xor r13, r13
.next:
cmp r12, rbx
jge .report
cmp byte [buffer + r12], 10
je .skip
inc r13
.skip:
inc r12
jmp .next
.report:
mov rdi, r13
call print_number
mov rdi, 10
call print_char
mov rax, 60
mov rdi, 0
syscallAll lessons in Functions, Stack and Strings
4Stack Frames and Recursion
Local VariablesFrames with RBPRecursionTwo Recursive CallsRecap: Digit Sum3The Calling Convention
Arguments in RegistersReturn ValuesCaller-Saved RegistersCallee-Saved RegistersRecap: Scoreboard6Project: Word Counter
The Read SyscallCount the CharactersPractice on your own: Assembly playground