ASM AT& T公司的语法 [英] ASM at&t syntax
问题描述
所以,我的问题,此code。这是一个程序,从二进制转换为数字deximal
so i've a problem with this code. This is a program for converting from binary to deximal numbers.
#include <cstdlib>
#include <iostream>
#include <cstdio>
char s[100];
int length;
int a;
int main(int argc, char *argv[])
{
system("cls");
printf("\n\n Vvedite stroky iz 0 i 1 (do 8 delementov) > ");
gets(s);
asm("leal s,%esi");
asm("movl 8,%ecx");
asm("movl 0,%edx");
asm("repeat:");
asm("movl 1,%eax");
asm("dec %ecx");
asm("rol %cl,%eax");
asm("cmpb 31,%esi");
asm("jnz x");
asm("add %eax,%edx");
asm("x: inc %esi");
asm("cmpb 0,$esi");
asm("jnz repeat");
asm("movl %edx,a");
printf("4islo %d",a);
return EXIT_SUCCESS;
}
它给了我:
分段故障(核心转储)
It gives me: "Segmentation fault (core dumped)"
请与本ASM部分提供帮助。
我认为这个问题在CMPB运营商。
Please help with this ASM part. I think the problem in the CMPB operator.
推荐答案
有一些问题与code - 我的系统上,它甚至没有通过编译/汇编运行。主要的问题是,你需要preFIX与 $
所有文字,否则汇编器总是假设一个内存访问:
There are some issues with that code - on my system, it did not even run through the compiler/assembler. The main issue is that you need to prefix all literals with $
, otherwise the assembler assumes a memory access:
asm("movl 8,%ecx"); // tries to access memory at address 8 => Segmentation fault
这必须是
asm("movl $8,%ecx"); // Moves literal 8 into ecx
相应调整所有其他指令。
Adjust all other instructions accordingly.
另一个问题是,下面的指令:
Another issue is the following instruction:
asm("cmpb 0,$esi"); // $esi is not a literal nor a register name
这必须是
asm("cmpb $0,(%esi)"); // compare value at address stored in %esi with literal 0 (end of string)
我建议你编译code调试信息,像
I suggest that you compile the code with debug information, like
$ g++ -g -o sample sample.c
有那么很容易调试程序:
It is then quite easy to debug the program:
$ gdb sample
(gdb) run
Starting program: sample
sh: cls: command not found
Vvedite stroky iz 0 i 1 (do 8 delementov) > 10101010
Program received signal SIGSEGV, Segmentation fault.
main (argc=1, argv=0x7fffffffe238) at sample.c:18
18 asm("movl 8,%ecx"); // current bit position
正如你可以看到,调试器显示您这引起了分段故障的指令。
As you can see, the debugger shows you the instruction which caused the segmentation fault.
更新
组装code。通过@Brett的建议这为我工作,使用单个 ASM
语句:
The assembly code which worked for me, using a single asm
statement as suggested by @Brett:
asm("leal s, %esi \n\t" // s => %esi
"movl $8, %ecx \n\t" // current bit position (+1)
"movl $0, %edx \n" // initialize result
"repeat: \n\t"
"movl $1, %eax \n\t" // Create bit mask in %eax
"dec %ecx \n\t" // Decrement rotation counter to shift mask bit to proper position
"rol %cl, %eax \n\t" // calculate value of current binary digit
"cmpb $0x31, (%esi) \n\t" // current digit == 1?
"jnz x \n\t" // no, skip
"add %eax, %edx \n" // add current value to %edx
"x: \n\t"
"inc %esi \n\t" // next address in input string
"cmpb $0, (%esi) \n\t" // end of string reached?
"jnz repeat \n\t" // no, continue
"movl %edx,a \n"); // store result in a variable
$ ./sample
Vvedite stroky iz 0 i 1 (do 8 delementov) > 10101010
4islo 170
这篇关于ASM AT&amp; T公司的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!