NASM组装有关搜索子字符串的问题

问题描述

嗨,大家好,我试图编写这段代码,检查输入的字符串中是否有字符。这是我的代码

我已经编写了打印语句,以查看程序的运行方式,但绝对不打印任何内容


extern getchar
extern printf
extern strlen
extern strchr


SECTION .data

    string1: db 0,0
  string2: db "it is there",10,0
  string3: db "not there",0
  string4: db "welp",0
  char:    db  "t",0
    count: dq 12
    forcount: dq 12 
  
    fmt: db "%s",0
  ;fmt2: db "%s",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  

我已经写了**所有这些代码**并且没有打印出来。有人可以告诉我我在这里做错了什么吗?请告诉我您是否要改写这个问题

解决方法

std                   ; auto decrement rsi
mov   rdx,[string1]
lea   rcx,[fmt]      ; a fmt string containing %lld
call  printf

鉴于您要为NASM进行编码,因此加载 string1 的地址需要变为:

mov rdx,string1

这是您在程序中重复多次的错误!!


还要注意,调用带有方向标志集的任何库函数绝不是一个好主意。许多编解码器都希望方向标记清晰明了。
而且,您当前的程序无论如何都不会使用该设置方向标志。


程序出口在哪里?
为什么会有2个SECTION .text声明?
...