I have the following code:
.section .data
input_prompt:
.asciz "Enter a string of 0s and 1s: "
invalid_message:
.asciz "ERROR: invalid input\n"
EEPrompt:
.asciz "Input accepted\n"
NotEEPrompt:
.asciz "Input rejected\n"
.section .bss
input:
.byte 0
.byte 0 # stores current character
.section .text
.globl main
main:
# PRINTS INPUT PROMPT
lea input_prompt, %rdi # point to our input prompt
call puts@PLT # outputs using PUTS
movl $0, %ebx # Initialize our state
lea input, %rsi # points to input buffer
movl $0, %edx # Initialize edx for conditional moves
read_loop:
# Read each character with call getchar@PLT
call getchar@PLT
# CHECKS FOR NEW LINE
cmpb $0x0A, %al # If a new line is detected, we are finished.
je input_finalize # End the loop
# We need to determine if the character we are processing is a '1' or a '0'
sub $0x30, %al # Convert ASCII to binary: '0' -> 0, '1' -> 1
cmovl %edx, %ebx # If %al is less than 0, set %ebx to %edx (invalid state)
cmovge %edx, %ebx # If %al is greater than or equal to 0, set %ebx to %edx
# Store our new and valid input
movb %al, (%rsi) # Store the character
inc %rsi # Increment the buffer
# Update the conditional move value for the next iteration
mov %ebx, %edx
# Jump table
jmp *jump_table(,%rbx,8)
jump_table:
.quad ee_state
.quad oe_state
.quad oo_state
.quad eo_state
ee_state:
movl $0, %edx # EE
jmp read_loop
oe_state:
movl $1, %edx # OE
jmp read_loop
oo_state:
movl $2, %edx # OO
jmp read_loop
eo_state:
movl $3, %edx # EO
jmp read_loop
invalid_input:
# Invalid Input Detected. Terminate the program.
lea invalid_message, %rdi # point to our invalidity message
call puts@PLT # outputs using PUTS
jmp exit_with_code
accepted_input:
# Print a message for ACCEPTED input
lea EEPrompt, %rdi # point to our acceptance message
call puts@PLT # outputs using PUTS
jmp exit_with_code
rejected_input:
# Print a message for REJECTED input
lea NotEEPrompt, %rdi # point to the error message
call puts@PLT
jmp exit_with_code
input_finalize:
# Add a newline character to the end of the input
movb $0x0A, (%rsi)
# Now, compare the %eax value to see if we are in EE.
cmpl $0, %edx # Check if we are in EE.
je accepted_input # IF SO, print our accepted message and exit
jg rejected_input # IF NOT, print our rejected message and exit
exit_with_code:
movq $60, %rax # syscall: exit
xor %rdi, %rdi # status code: %rdi (0 or 1)
syscall
It resembles a DFA system, my program is supposed to calculate if an inputted binary string has an even number of 1's and 0's. However, I keep getting a segmentation fault when run and I'm not sure why. I'd prefer to keep my jump tables, and my getchar/puts statements, so how can I resolve my segmentation error given that?
