英特尔汇编编程
class MyString{
char buf[100];
int len;
boolean append(MyString str){
int k;
if(this.len + str.len>100){
for(k=0; k<str.len; k++){
this.buf[this.len] = str.buf[k];
this.len ++;
}
return false;
}
return true;
}
}
上面的内容是否可以翻译为:
start:
push ebp ; save calling ebp
mov ebp, esp ; setup new ebp
push esi ;
push ebx ;
mov esi, [ebp + 8] ; esi = 'this'
mov ebx, [ebp + 14] ; ebx = str
mov ecx, 0 ; k=0
mov edx, [esi + 200] ; edx = this.len
append:
cmp edx + [ebx + 200], 100
jle ret_true ; if (this.len + str.len)<= 100 then ret_true
cmp ecx, edx
jge ret_false ; if k >= str.len then ret_false
mov [esi + edx], [ebx + 2*ecx] ; this.buf[this.len] = str.buf[k]
inc edx ; this.len++
aux:
inc ecx ; k++
jmp append
ret_true:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret true
ret_false:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret false
我这里最大的困难是弄清楚将什么推入堆栈以及指针的数学运算。
笔记: 我不允许使用全局变量,并且必须假设 32 位整数、16 位字符和 8 位布尔值。
class MyString{
char buf[100];
int len;
boolean append(MyString str){
int k;
if(this.len + str.len>100){
for(k=0; k<str.len; k++){
this.buf[this.len] = str.buf[k];
this.len ++;
}
return false;
}
return true;
}
}
Does the above translate to:
start:
push ebp ; save calling ebp
mov ebp, esp ; setup new ebp
push esi ;
push ebx ;
mov esi, [ebp + 8] ; esi = 'this'
mov ebx, [ebp + 14] ; ebx = str
mov ecx, 0 ; k=0
mov edx, [esi + 200] ; edx = this.len
append:
cmp edx + [ebx + 200], 100
jle ret_true ; if (this.len + str.len)<= 100 then ret_true
cmp ecx, edx
jge ret_false ; if k >= str.len then ret_false
mov [esi + edx], [ebx + 2*ecx] ; this.buf[this.len] = str.buf[k]
inc edx ; this.len++
aux:
inc ecx ; k++
jmp append
ret_true:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret true
ret_false:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret false
My greatest difficulty here is figuring out what to push onto the stack and the math for pointers.
NOTE:
I'm not allowed to use global variables and i must assume 32-bit ints, 16-bit chars and 8-bit booleans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您有一项作业,要创建执行字符串追加操作的 Intel x86 汇编代码。
如果这是真的,那么检查 C 编译器(例如 Intel C 编译器或 Microsoft VC++ Express 编译器)的反汇编输出可能会对您有所帮助。编译一些非常简单的 C 代码,然后反汇编并查看结果。
编译器的工作方式通常与人类编码员的工作方式略有不同,但根据我的经验,弄清楚生成的代码中发生了什么并不困难。您可以看到 ASM 代码如何管理堆栈、如何完成指针算术、如何以及何时初始化寄存器、使用哪些寄存器以及如何使用等等。
一个简单的 C 例程可能是您自己的 strlen 版本。
然后,增加复杂性或更改内容,再次编译和反汇编,看看会发生什么。例如,创建一个返回字符串中最后一个字符的 C 例程,而不是 strlen。
或者,如果您只是想要答案,可以尝试 Codecodex。
:)
Sounds like you have an assignement, to create Intel x86 assembly code that performs a string append operation.
If that's true, It might be instructive for you to examine the disassembled output of a C compiler - like the Intel C compiler, or the Microsoft VC++ Express compiler. Compile some really simple C code, then disassemble and look at the results.
Compilers usually do things a little differently than a human coder would, but in my experience it's not difficult to figure out what's happening in the generated code. You can see how the ASM code manages the stack, how pointer arithmetic in done, how and when registers are initialized, which registers are used and how, and so on.
A simple C routine might be, your own version of strlen.
Then, add complexity or change things, compile and disassemble again, and see what happens. Like, instead of strlen, make a C routine that returns the last char in a string.
or, if you just want the answer, you could try Codecodex.
:)