本文介绍了如何将这段汇编代码翻译成c?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 C 中,我有以下结构:
In C I have the following struct:
typedef struct _Node{
int data;
struct _Node *left;
struct _Node *right;
} Node;
以及以下汇编代码:
.section .text
.global _start
_start:
mov $8, %esi
mov $A, %rdi
call func
movq $60, %rax
movq $0, %rdi
syscall
func:
pushq %rbp
movq %rsp, %rbp
cmp (%rdi), %esi
jne continue
mov $1, %eax
jmp finish
continue:
cmpq $0, 4(%rdi)
je next
pushq %rdi
mov 4(%rdi), %rdi
call func
pop %rdi
cmp $1, %eax
je finish
next:
cmpq $0, 12(%rdi)
je fail
pushq %rid
mov 12(%rdi), %rdi
call func
pop %rdi
cmp $1, %eax
je finish
fail:
mov $0, %rax
finish:
leave
ret
现在尝试用 C 编写它,我有一个问题:
Now trying to write it in C I have a question:
__long__ func ( Node *root, __int__ x){
if (root->data == __x__ )
return 1;
if (root->left != null)
if (_____??_____)
return ___ func(root->left, x)____;
if (root->right != null)
return ____func(root->right, x)____;
}
为什么我们有 2 个 if-if ?如果 left 不为 null,则汇编代码会使用左子调用该函数,并且不会进行其他条件检查(即 cmp 调用).
Why we have 2 if-if inside each other? If left isn't null the assembly code calls the function with the left son and doesn't do another condition check (ie cmp call).
推荐答案
第二个 if
是如果 func
成功则返回的那个.
The second if
is the one that returns if func
was successfull.
bool func(Node *nd, int x)
{
if (nd->data == x)
return true;
if (nd->left)
{
if (func(nd->left, x) == true)
return true;
}
if (nd->right)
return func(nd->right, x);
return false;
}
我选择 bool
因为它在这个函数的上下文中是有意义的,即使 int
或 long
或其他也可能是有效的.
I choose bool
because it makes sense in the context of this function, even though int
or long
or others may also be valid.
这篇关于如何将这段汇编代码翻译成c?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!