0

I'm trying to do a quick test program in x86 assembly that takes a string and just outputs it again, that also detects if there is a space and displays some text. This program is mainly so I can figure out detection of the space character.

LC0:
    .ascii "Enter a string: \0"
LC1:
    .ascii "%[^\n]\0"
LC2:
    .ascii "Your string is:\12\0"
LC4:
    .ascii "%c\12\0"  # print out the character 
LC6:
    .ascii "Yeehaw!\12\0" # test message
    
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    
    pushl   %ebx
    subl    $112, %esp
    
    movl    $LC0, (%esp)
    call    _puts                # similar to _printf function
    leal    8(%esp), %eax
    movl    %eax, 4(%esp)
    
    movl    $LC1, (%esp)
    call    _scanf               # get string 
    
    movl    $LC2, (%esp)
    call    _printf              # printout the output text of LC2
    
            ###### A[i] ###### ebx is the index 
    movl    $0, %ebx             #index of array i=0    
    jmp     .L2
    
.L1: #TEXT PORTION
    movsbl  %al, %eax           # eax =FFFFFFXX  XX means for each character print out the input character 
    incl    %ebx                # next index of array 
    
    movl    %eax, 4(%esp)       # print out from the first element of the array
    movl    $LC4, (%esp)
    call    _printf
        
.L2: #TEXT PORTION
    movzbl  8(%esp,%ebx), %eax  # the value of esp+8+0 strating address of the array to eax
    
    #Problem lines
    cmpb 0x20, %al # compare eax to see if it is a space
    jne .EndOfLine
    
    #should only run if %al is a space
    movl    $LC6, (%esp) # test message to check if properly detecting spaces
    call _printf
    
.EndOfLine:
    testb   %al, %al            # if eax is not equal to zero jump to L3, if is zero menas end of the string 
    jne     .L1 
    
    movl    $0, %eax
    addl    $112, %esp
    leave
    ret

Without cmpb 0x20, %al my program works just fine (if input is "Hello", will print "Hello"), but as soon as I add that line it doesn't print anything and just goes to the end of the program. Anyone know why this is happening?

MHasting
  • 1
  • 1
  • 1
    Shouldn't that be `cmpb $0x20, %al` ? You want to compare against an immediate, not memory. – Michael Dec 09 '21 at 06:57
  • Goes to the end of the program?? It should segfault on access to absolute address `0x20`, and I don't see you using `sigaction` to install a handler for SIGSEGV with "the end of your program" as the handler address. If you mean your program exits, then yes. – Peter Cordes Dec 09 '21 at 07:12
  • Thank you Michael, I'm not quite sure how I completely forgot to put $ before the value. – MHasting Dec 09 '21 at 07:17

0 Answers0