汇编中字符串的长度对程序的工作方式感到困惑

如何解决汇编中字符串的长度对程序的工作方式感到困惑

我有一个汇编程序,该程序将一个字符串的长度写入ax寄存器中,但是我对某些指令有些困惑。

include \masm32\include64\masm64rt.inc
.data
string db "mama",0        ; so here I declared a string "mama". What happens in memory?
.code
 main proc
xor ax,ax                 ; here I initialize ax with 0.
lea rsi,string            ; here,I move in rsi register the adress of string,right? But how the string is stored in memory? Now in rsi I have the adress of the first char "m" of "mama" string?
 @@:                       ; this sign creates an anonymous label
cmp byte ptr [rsi],0      ; so this says compare 0 with with 1 byte found at the adress pointed to by rsi,right? But I still don't get it. Why 1 byte? rsi is pointing to the first char or to the whole string?
jz @F                      ; jump if zero to the nearest @@ (forward)
inc ax                     ; so now i'm pointing to the first character so ax=1
inc rsi                    ; here what happen? The pointer is incremented to point to the second char from string?
jmp @B                     ; jump to the nearest @@ (backward)
 @@:
invoke ExitProcess,0 ; invoke ExitProcess API
ret
 main endp
end

我的困惑是我不确定我是否在考虑该程序的正确工作方式。我在想这个吗?

解决方法

inc ax将16位值加1-处理器不知道ax用于什么,但它知道这是16位加法。

inc rsi将64位值加1-处理器不“知道”这是整数还是指针,但它确实知道这是64位加法。

此程序(尽管只是一个main而没有任何功能)类似于一个strlen函数:

short strlen ( char *p ) {
    short count = 0;
    while ( *p != '\0' ) {
        count++;                // increment 16-bit counter
        p++;                    // increment 64-bit pointer by adding 1 to it
    }
    return count;
}

处理器将指针视为整数。取消引用时引用内存位置的整数。用汇编语言将指针递增1使其指向内存的下一个字节-内存位置具有整数编号的地址,因此两个连续的内存地址的值将相差1。

请注意,由于长度计数器使用了短的16位数据类型,如果字符串的实际长度> = 32768(如果已理解带符号的short,则此程序将出现溢出错误)或> = 65536(如果使用unsigned short代替short)。

,
string db "mama",0

4个字节的0x6d 0x61 0x6d 0x61('妈妈')存储在程序内存的数据段中的某个位置。 string将第一个字节的地址存储在数据段中,即“ m”。

xor ax,ax

lea rsi,string

我认为操作应为lea rsi,[string]。 (编辑:正如下面的评论中的Peter Cordes一样,在MASM汇编程序中,这种语法很好)

string指向第一个字符的地址。现在rsi指向相同的地址。

@@:                       ; this sign creates an anonymous label

cmp byte ptr [rsi],0

rsi指向整个字符串的开头。比较操作将rsi处的一个字节与零进行比较。如果为零,则假定字符串的结尾并跳转以退出:

jz @F                      ; jump if zero to the nearest @@ (forward)

如果rsi的值不为零:

inc ax

请记住,我们将字符串的长度存储在ax中。因此,对于每个有效字符,我们将ax加1。

inc rsi

jmp @B                     ; jump to the nearest @@ (backward)

rsi指向下一个字符('a'),然后跳转到@@。第一个@@之后的代码将再次检查下一个字符('a')是否为零,并将计数(ax)加1,因此ax将变为2。这将继续直到到达0时,程序假定字符串结尾。

@@:

invoke ExitProcess,0 ; invoke ExitProcess API

ret

 main endp

end

退出代码。

旁注:您可以在开头使用带有断点的gdb之类的程序来完成每个步骤。使用info registers命令可以检查它们的值。向Google询问更高级的命令/方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-