String 从程序集中的读取输入中删除尾随换行字符

String 从程序集中的读取输入中删除尾随换行字符,string,assembly,stdin,nasm,String,Assembly,Stdin,Nasm,我最近一直在自学汇编,我认为NASM汇编程序及其语法是最有效和最容易使用的。我目前从事标准输入和输出工作;但是,由于我需要从正在读取的字符串中删除换行字符(回车符、换行符、换行符、-0xd、0xc和0xa),所以我不知所措。考虑以下事项: section .data ;; ... section .bss input: resb 255 .len: equ $ - input section .text global _start _st

我最近一直在自学汇编,我认为NASM汇编程序及其语法是最有效和最容易使用的。我目前从事标准输入和输出工作;但是,由于我需要从正在读取的字符串中删除换行字符(回车符、换行符、换行符、-0xd、0xc和0xa),所以我不知所措。考虑以下事项:

    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
目前,我希望剥离尾随换行字符。考虑下面的伪代码:

if the last character in `input` is 0xa or 0xc or 0xd:
    subtract the last character from `input`
repeat until false
我很可能已经讲清楚了,但这里有一个与上述伪代码等效的Python:

if the last character in `input` is 0xa or 0xc or 0xd:
    subtract the last character from `input`
repeat until false
当输入[-1]为“\r”或输入[-1]为“\f”或输入[-1]为“\n”时:
输入=输入[:-1]

这不是特别优雅或高效,但它可能提供了一个起点:

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$

我试图设计一个子程序,删除字符串的最后一个字符,如果该字符是回车符、换行符或换行符;它将循环直到所述条件为假。谢谢!有趣的方法。。。我对这真的很陌生。