在AT& T x86程序集中查找转义字符 [英] Finding escape characters in AT&T x86 assembly

查看:51
本文介绍了在AT& T x86程序集中查找转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:我有以下汇编代码,目的是循环输入字符串,并计算遇到的转义字符'%'的数量:

Question 1: I have the following assembler code, whose purpose is to loop through an input string, and count the number of escape characters '%' it encounters:

.globl sprinter

.data

.escape_string: .string "%"

.num_escape: .long 0

.num_characters: .long 0

.text

sprinter:
    pushl %ebp
    movl %esp,%ebp

    movl 8(%ebp),%ecx # %ecx = parameter 1

loop:
    cmpb $0, (%ecx) # if end of string reached
    jz exit
    cmpl $.escape_string,(%ecx) # if escape character found
    je increment
    back:
        incl .num_characters
        incl %ecx
        jmp loop

increment:
    incl .num_escape
    jmp back # jump to 'back'

exit:
    movl .num_escape, %eax # return num_escape

    popl %ebp
    ret

此汇编代码与以下C代码一起编译:

This assembly code is compiled together with the following C code:

#include <stdio.h>
extern int sprinter (char* string);
int main (void)
{
    int n = sprinter("a %d string of some %s fashion!");
    printf("value: %d",n);
    return 0;
}

运行此代码的预期输出为 value:2 (因为字符串中有两个'%'字符),但是它返回 value:0 ,这意味着以下行失败(因为它永远不会增加计数器):

The expected output from running this code is value: 2 (because there are two '%' characters in the string), but it returns value: 0, meaning the following line fails (because it never increments the counter):

cmpl $.escape_string,(%ecx) # if escape character found

我使用错误的字符串比较方法吗?外循环工作正常,.num_characters正确包含了我字符串中的字符数.我为一个简单的C程序生成了一些汇编代码,该程序将字符串"hello"与"hello2"进行了比较,这是相关的代码:

Am I using the wrong method of comparing for the string? The outer loop works fine, and .num_characters correctly contains the number of characters in my string. I generated some assembly code for a simple C-program that compared a string "hello" to "hello2", and this is the relevant code:

.LC0:
    .string "hello"
.LC1:
    .string "hello2"

...
movl    $.LC0, -4(%ebp)
cmpl    $.LC1, -4(%ebp)

它看起来与我尝试过的非常相似,不是吗?

It looks very similar to what I tried, no?

问题2 .该代码是用汇编语言编写的简化sprintf函数的一部分.这意味着第一个参数应该是结果字符串,第二个参数是格式.如何将一个字节字符从一个寄存器中的当前位置复制到另一个寄存器中的当前位置?假设我们已将参数分配到两个寄存器中:

Question 2. This code is part of what is going to be a simplified sprintf-function written in assembly. This means the first parameter should be the result string, and the second parameter is the formatting. How do I copy a byte character from our current position in one register to our current position in another register? Let's assume we've assigned our parameters into two registers:

movl 8(%ebp),%edx # %edx = result-string
movl 12(%ebp),%ecx # %ecx = format-string

我在循环中尝试了以下操作:

I tried the following in the loop:

movb (%ecx), %al
movb %al, (%edx) # copy current character to current position in result register
incl %ecx
incl %edx

但是结果字符串仅包含 a (我字符串中的第一个字符),而不是我期望的完整字符串.

But the result string just contains a (the first character in my string), and not the full string as I expected.

感谢所有帮助,因为此比较问题(问题1)目前使我陷于困境.

All help appreciated because this comparison problem (question 1) is currently keeping me stuck.

推荐答案

关于问题1,看来您正在比较单字节字符,因此在检查转义字符时,"cmpl"应为"cmpb".您还需要将您的角色加载到寄存器中.我对AT& T组装不是很熟悉,所以我希望这是正确的.

In regards to question 1, it appears that you are comparing single byte chars so 'cmpl' should be 'cmpb' when checking for the escape character. You will also need to load your character into a register. I'm not really familiar with AT&T assembly, so I hope this is correct.

循环前:

movb .escape_string, %al

比较:

cmpb %al, %(ecx)

这篇关于在AT&amp; T x86程序集中查找转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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