assembly - 如何在 Assembly 中使用 scasb 查找字符串长度

标签 assembly x86 nasm strlen

我有一个字符串,正在尝试找出它的长度。我正在使用 rw32-2017。我尝试使用 repe scasb 扫描字符串,但它根本没有改变 ZF。 有没有更简单的方法来查找字符串的长度?

我的程序:

%include "rw32-2017.inc"

section .data
    ; write your data here
    string1 db "how long is this string",0

section .text
main:
    mov ebp, esp
    
    ; write your code here
    mov esi,string1
    mov eax,0
    mov ecx,50      ;max length of string is 50
    mov ebx,ecx
    cld
    repe scasb 
    sub ebx, ecx
    mov eax,ebx
    call WriteUInt8

    ret

最佳答案

对于 SCAS,您应该将字符串的地址放在 EDI 中,而不是 ESI。而不是 REPE 你想要 REPNE (当不等于和 ecx!=0 时重复)。

mov edi,string1
mov eax,0
mov ecx,50 ;max lenght of string is 50
mov ebx,ecx
cld
repne scasb   ; find AL (0), starting at [ES:EDI]

关于assembly - 如何在 Assembly 中使用 scasb 查找字符串长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292525/

相关文章:

assembly - 为什么 rust 在有和没有 pub 和 #[no_mangle] 的情况下会产生完全不同的 assembly ?

c - Sandy Bridge 上的 32 字节存储转发

windows - NASM 教程使用 int 80h,但这不适用于 Windows

assembly - 是什么导致这个程序出现段错误?

virtualization - x86 虚拟化指令集(VT-x、AMD-V)是否有其他用途?

assembly - 实模式下的自定义 IRQ 处理程序

assembly - YASM/NASM x86 汇编中立即数与方括号的基本使用

assembly - VA(虚拟地址)和 RVA(相对虚拟地址)

c - 如何在 C 中实现类似 nasm 的结构?

c++ - 在堆栈顶部搜索线程启动参数