遍历汇编中的字符串(NASM) [英] Iterate over strings in assembly (NASM)

查看:598
本文介绍了遍历汇编中的字符串(NASM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算NASM汇编语言中字符串argv [1]的长度.我认为我走在正确的轨道上.我已经移动了argv [1]的地址来注册eax,现在我想逐字节地遍历它并与空字符串终止符进行比较.

I am trying to count the length of the string argv[1] in NASM assembly language. I think I'm on the right track. I have moved the address of argv[1] to register eax and now I want to move through it byte by byte and compare to the null string terminator.

每次我在空比较上运行它segfaults的代码.我的内存索引编制不正确吗?

Everytime I run the code it segfaults on the null comparison. Am I not getting the memory indexing correct?

*免责声明:这只是一项大型家庭作业的一小部分.

*Disclaimer: This is a small part of a large homework assignment.

segment .bss

N: resd 1                 ;counter for size of argv[1]

segment .text
  global asm_main

asm_main:
  enter 0,0               ;setup
  pusha                   ;save all registers

  mov eax, dword [ebp+8]  ;argc to eax

  mov ebx, dword [ebp+12] ; address of argv to ebx
  mov eax, dword [ebx+4]  ; address of argv[1] to eax

  mov [N], dword 0        ; N = 0 

  .loop:

    add eax, [N]          ; advance index by N
    cmp eax, dword 0      ; check for end of string
    je .endloop           ; break out of the loop if we're done


    add [N], dword 1      ; N++    

    jmp .loop             ; loop back for next char

  .endloop:

popa
mov eax, 0
leave
ret

推荐答案

经过一些提示和gdb的帮助,循环现在看起来像这样:

After a few hints and the help of gdb, the loop now looks like this:

  mov [N], dword 0        ; N = 0 

  .loop:

    cmp [eax], byte 0     ; check for end of string
    je .endloop    

    add eax, dword 1      ; advance index by 1 byte
    add [N], dword 1      ; N++    

    jmp .loop             

  .endloop:

使用N来增加索引是愚蠢的.我需要增加1.

Using N to increment the index was silly. I needed to increment by 1.

这篇关于遍历汇编中的字符串(NASM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆