Reputation: 477
I have recently been teaching myself assembly, and I decided that the NASM assembler and its syntax are the most efficient and easiest to use. I am currently working with standard input and output; however, I am at a loss as I need to remove line-breaking characters (carriage return, form-feed, newline, - 0xd, 0xc and 0xa, respectively) from a string that I'm reading. Consider the following:
section .data
;; ...
section .bss
input: resb 255
.len: equ $ - input
section .text
global _start
_start:
;; Display message prompting for input, then...
mov edx, input.len
mov ecx, input
mov ebx, 0
mov eax, 3
int 0x80
Presently, I wish to strip trailing line-breaking characters. Consider the following pseudo-code:
if the last character in `input` is 0xa or 0xc or 0xd:
subtract the last character from `input`
repeat until false
I have most likely made myself clear, but here is a Python equivalent to the above pseudo-code:
while input[-1] is "\r" or input[-1] is "\f" or input[-1] is "\n":
input = input[:-1]
Upvotes: 2
Views: 2811
Reputation: 38442
this isn't particulary elegant or efficient, but it might provide a starting point:
jcomeau@intrepid:/tmp$ cat test.nasm ; nasm -f elf -o test.o test.nasm; ld -o test test.o; ./test
section .bss
input: resb 255
.len: equ $ - input
section .text
global _start
_start:
;; Display message prompting for input, then...
mov edx, input.len
mov ecx, input
mov ebx, 0
mov eax, 3
int 0x80 ;read in from stdin
call rstrip
mov edx, eax ;count
mov ebx, 1 ;stdout
mov eax, 4
int 0x80 ;write out
mov eax, 1
xor ebx, ebx
int 0x80
rstrip:
dec eax ;convert 1-based length to 0-based pointer
.loop:
cmp byte [ecx + eax], 0xa
je .chop
cmp byte [ecx + eax], 0xc
je .chop
cmp byte [ecx + eax], 0xd
je .chop
.done:
inc eax ;convert pointer back to length
ret
.chop:
mov byte [ecx + eax], 0
dec eax
jns .loop
jmp .done
this is a test
this is a testjcomeau@intrepid:/tmp$
Upvotes: 2