Search code examples
assemblyattfish

Don't line break in shell after executing asm programm. [AT&T]


Like in the title said the asm programm should not automatically break a new line after executing it. My programm is a simplified version of the "echo" command in linux. It prints the arguments given by the user. If the users first argument is '-n' the prompt should be to the right of the output and not break the line. So my question is how do i force NOT breaking a line in AT&T after executing?

.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

Here is the output of my program

Output of $PS1, $PS1 and the normal echo command:

enter image description here

Count of bytes is 6 of the word "Hello" so with a null termination

Count of bytes


Solution

  • Your program is working.

    Like zsh, fish does a trick to get your prompt onto a new line after a programs output, even if it doesn't end in a newline.

    When that happens, a "⏎" is left at the end of the line. (zsh leaves a "%" IIRC)

    So those times that you can see a "⏎", your program already didn't print a newline. But to avoid confusion between the program's output and the shell's prompt, it's nice to always have the prompt on a new line, so this trick is used.

    And fish doesn't use $PS1 or $PS2, so those are useless to show. Instead it uses the fish_prompt function.