Не прерывайте строку в оболочке после выполнения программы asm. [AT & T] - PullRequest
0 голосов
/ 12 января 2020

Как и в заголовке, программа asm не должна автоматически переносить новую строку после ее выполнения. Моя программа является упрощенной версией команды "echo" в linux. Он печатает аргументы, данные пользователем. Если первым аргументом пользователя является '-n', приглашение должно быть справа от вывода и не прерывать строку. Поэтому мой вопрос заключается в том, как заставить принудительно НЕ прерывать строку в AT & T после выполнения?

.code32
.section .data
    lf: .ascii "-n"             # Lf = line feed
    withoutLF: .long 0             # helper flag
    counter: .long 0            # is the counter of bytes of an argument
    arguments: .long 0          # number of arguments
    white_space: .ascii " "     # is white space

.section .text

.globl _start

_start:
    movl (%esp), %esi           # gets the argument count of the stack to ESI
    movl %esi, arguments        # moves argument count to arguments variable
    decl arguments              # decrement counter
    addl $8, %esp               # move the esp to the actual arguments

    ### check if first argument is '-n' ###

    movl (%esp), %esi           # move first string to ESI
    movl $lf, %edi              # move second string to EDI
    movl $2, %ecx               # number of  bytes which should be compared
    cld                         # clear Flag D, wihtout clearance the compare ill occur in inverse order 
    rep cmpsb                   # the actual comparing of the 2 strings
    jz _without_lf              # if the same jump to _without_lf

_arg:
    movl (%esp), %esi           # moves argument of stack to ESI
    cmpl $0, arguments          # compares if there any arguments left
    je _exit                    # out of arguments --> exit progamm
    jmp _print_white_space      # if NOT out of arguments --> print a white space


_numberOfBytes:
    lodsb                       # load string in bytes from ESI --> saves byte in EAX
    cmpb $0, %al                # compare if byte is equal 0
    je _print_arg               # jumps to section to print the actual argument
    incl counter                # increment the counter (number of bytes)
    jmp _numberOfBytes          # jump to the beginning of _numberOfBytes (to read the left bytes)

_print_arg:
    movl counter, %edx          # EDX message length 
    movl $1, %ebx               # EBX = file descriptor (1 = stdout)
    movl (%esp), %ecx           # ECX = address of message
    movl $4, %eax               # syscall number (4 = write)
    int $0x80                   # call kernel by interrupt

    addl $4, %esp               # move ESP to next argument (1 argument = 4 bytes)
    decl arguments              # decrement the number of arguments
    movl $0, counter            # reset the counter of bytes
    jmp _arg                    # jump to the top of _arg

_print_white_space:
    movl $1, %edx               # EDX message length
    movl $1, %ebx               # EBX = file descriptor (1 = stdout)
    movl $white_space, %ecx     # ECX = address of message (startaddress)
    movl $4, %eax               # syscall number (4 = write)
    int $0x80                   # call kernel by interrupt
    jmp _numberOfBytes          # jump to _numberOfBytes --> still arguments left to print

_without_lf:
    addl $4, %esp               # move ESP to next argument of the stack
    decl arguments              # decrement arguments counter --> '-n' is not counted, just used
    incl withoutLF              # move 1 to withLF (helper flag)
    jmp _arg

   _exit:                       # exit the program
     movl $0, %ebx
     movl $1, %eax
     int $0x80

Вот вывод моей программы

Here is the output of my program

Вывод $ PS1, $ PS1 и обычной команды echo:

enter image description here

Количество байтов равно 6 слова "Hello", поэтому с нулевым окончанием

Count of bytes

1 Ответ

2 голосов
/ 16 января 2020

Ваша программа работает.

Как и zsh, fi sh делает трюк, чтобы вывести ваше приглашение на новую строку после вывода программы, даже если она не заканчивается новой строкой.

Когда это происходит, в конце строки остается «⏎». (zsh оставляет "%" IIR C)

Таким образом, в те моменты, когда вы видите "⏎", ваша программа уже не печатала перевод строки. Но чтобы избежать путаницы между выводом программы и приглашением оболочки, желательно всегда иметь приглашение в новой строке, поэтому этот прием используется.

И fi sh не использует $ PS1 или $ PS2, так что это бесполезно показывать. Вместо этого он использует функцию fish_prompt.

...