In NASM Assembly, problem is to determine if a user's dynamic input, formatted by a null-
terminated character, is or is not a palindrome. MUST use the stack (push & pop) to do so, with
only commands, mov, cmp, loop, and jump. I keep getting segfaults and early exits Current error is
Inferior 1 (process 3125) exited normally
---------------------------
global main ; exposes program entry point to the linker
extern printf ; declare external function
extern scanf ; declare external function
section .text ; start of code segment
main:
push rbp ; preserve base pointer
mov rbp,rsp ; copy stack pointer to base pointer
; Prompt for entry of a word to test if a palindrome
mov rdi,prompt ; prompt format string for printf
call printf ; call printf function from C library
; Input a word to input buffer (buf)
mov rdi,fmt_str ; format string for scanf
mov rsi,buf ; address of input buffer for scanf
call scanf ; call scanf function from C library
; TODO: Check null-terminated string at buf to see if a palindrome
L1:
; Is string a palindrome?
xor rax,rax ; clear index
mov rax,[buf-3] ; Address of last character
dec rsi ; index of last char
mov sil,1 ; assume input is palindrome
;Check for palidrome
cmp rdi,rsi ; compare indices
jge L3 ; exit loop if indices have crossed
mov dl, byte[buf+rdi] ; read from start of string
mov dh, byte[buf,rsi] ; read byte from end of string
cmp dl, dh ; compare bytes
jne L3 ; exit loop if bytes differ (NOT palindrome)
inc rdi ; advance index from start
dec rsi ; advance index from end
jmp L1 ; check_palindrome, repeat loop
L3: ;not_palindrome:
mov al,0 ; indicate input is NOT a palindrome
mov sil,byte[str_is_not_palindrome] ; save to memory
L2: ;Is a palindrome
mov al,1 ; indicate input is a palindrome
mov sil,byte[str_is_not_palindrome] ; save to memory
; TODO: Move either str_is_palindrome or str_is_not_palindrome
; to RDI to output appropriate message
; Output message, address of output string expected in RDI
call printf ; call printf function from C library
pop rbp ; restore base pointer
mov rax, 0 ; exit status (0 = success)
ret
section .data ; start of initialized data segment
prompt db "Enter a word: ",0 ; Prompt for entry
buf_confirm db "You entered %s.",0xa,0
str_is_palindrome db "This word is a palindrome.",0xa,0
str_is_not_palindrome db "This word is not a palindrome.",0xa,0
fmt_str db "%s",0 ; Input format string for scanf
section .bss ; start of uninitialized data segment
buf resb 100 ; input buffer, for entry of a word

In NASM Assembly problem is to determine if a users dynami.pdf

  • 1.
    In NASM Assembly,problem is to determine if a user's dynamic input, formatted by a null- terminated character, is or is not a palindrome. MUST use the stack (push & pop) to do so, with only commands, mov, cmp, loop, and jump. I keep getting segfaults and early exits Current error is Inferior 1 (process 3125) exited normally --------------------------- global main ; exposes program entry point to the linker extern printf ; declare external function extern scanf ; declare external function section .text ; start of code segment main: push rbp ; preserve base pointer mov rbp,rsp ; copy stack pointer to base pointer ; Prompt for entry of a word to test if a palindrome mov rdi,prompt ; prompt format string for printf call printf ; call printf function from C library ; Input a word to input buffer (buf) mov rdi,fmt_str ; format string for scanf mov rsi,buf ; address of input buffer for scanf call scanf ; call scanf function from C library ; TODO: Check null-terminated string at buf to see if a palindrome L1: ; Is string a palindrome? xor rax,rax ; clear index mov rax,[buf-3] ; Address of last character dec rsi ; index of last char mov sil,1 ; assume input is palindrome ;Check for palidrome cmp rdi,rsi ; compare indices jge L3 ; exit loop if indices have crossed mov dl, byte[buf+rdi] ; read from start of string mov dh, byte[buf,rsi] ; read byte from end of string cmp dl, dh ; compare bytes jne L3 ; exit loop if bytes differ (NOT palindrome) inc rdi ; advance index from start dec rsi ; advance index from end jmp L1 ; check_palindrome, repeat loop L3: ;not_palindrome: mov al,0 ; indicate input is NOT a palindrome mov sil,byte[str_is_not_palindrome] ; save to memory L2: ;Is a palindrome mov al,1 ; indicate input is a palindrome mov sil,byte[str_is_not_palindrome] ; save to memory
  • 2.
    ; TODO: Moveeither str_is_palindrome or str_is_not_palindrome ; to RDI to output appropriate message ; Output message, address of output string expected in RDI call printf ; call printf function from C library pop rbp ; restore base pointer mov rax, 0 ; exit status (0 = success) ret section .data ; start of initialized data segment prompt db "Enter a word: ",0 ; Prompt for entry buf_confirm db "You entered %s.",0xa,0 str_is_palindrome db "This word is a palindrome.",0xa,0 str_is_not_palindrome db "This word is not a palindrome.",0xa,0 fmt_str db "%s",0 ; Input format string for scanf section .bss ; start of uninitialized data segment buf resb 100 ; input buffer, for entry of a word