Reputation:
Hi guys so Im trying to write this code that checks if a character is in an inputted string. This is my code
I have written print statements to see how my program is running but it prints absolutely nothing.
extern getchar
extern printf
extern strlen
extern strchr
SECTION .data
string1: db 0,0,0,0,0,0,0,0,0,0,0,0
string2: db "it is there",10,0
string3: db "not there",10,0
string4: db "welp",10,0
char: db "t",10,0
count: dq 12
forcount: dq 12
fmt: db "%s", 10, 0
;fmt2: db "%s", 10, 0
SECTION .text
global main
main:
SECTION .text
global main
main:
sub rsp, 32 ; shadow space
mov rdi, string1 ; printing to
cld ; clear direction flag
.while:
cmp qword [count], 0 ; only get a char while counter is > 0
jne .continue
jmp .done
.continue:
mov rax, 0 ; clear rax before we get a char
call getchar
cmp eax, 10 ; newline
jne .continue2 ; stop collecting on new lines
jmp .done
.continue2:
stosb ; puts al into [rdi] and then increments rdi
sub qword [count], 1
jmp .while
.done:
mov byte [rdi+1], 0 ; don't forget to 0 terminate your strings
;lea rdx, [string1] ;it is here so fa
;lea rcx, [fmt]
;call printf
lea rsi, [string1]
lea rcx, [string1]
call strlen
lea rcx, [rax - 1] ; we need to decrement rax by 1 since strings are 0 indexed
add rsi, rcx ; index to end of string (- 1)
std ; auto decrement rsi
mov rdx, [string1]
lea rcx, [fmt] ; a fmt string containing %lld
call printf
;using it
mov rcx, string1
mov rdx,[char]
call strchr
sub rax, rcx
cmp rax, 0x00
je .no
jne .yes
jmp .welp
.yes:
mov rdx, [string2]
lea rcx, [fmt] ; a fmt string containing %lld
call printf
.no:
mov rdx, [string3]
lea rcx, [fmt] ; a fmt string containing %lld
call printf
.welp:
mov rdx, [string4]
lea rcx, [fmt] ; a fmt string containing %lld
call printf
I have written **all this code ** and its not printing. Can someone please tell me what Im doing wrong here? Please tell me if you want me to rephrase the question
Upvotes: 0
Views: 262
Reputation: 39205
std ; auto decrement rsi mov rdx, [string1] lea rcx, [fmt] ; a fmt string containing %lld call printf
Given that you're coding for NASM, loading the address of the string1 needs to become:
mov rdx, string1
This is an error that you repeat numerous times in your program!!!
Also note that it's never a good idea to call any library function with the direction flag set. Many codez expect the direction flag to be clear.
Moreover, your current program doesn't use that set direction flag anyway.
Where is your program exit?
Why are there 2 SECTION .text
declarations?
...
Upvotes: 1