执行的指令数为Hello World程序NASM汇编和C不同 [英] Number of executed Instructions different for Hello World program Nasm Assembly and C
问题描述
我有一个简单的调试器(使用ptrace的: http://pastebin.com/D0um3bUi )来算对于给定的输入可执行program.It执行的指令数使用ptrace的单步执行模式计数指令
有关,当程序1)的可执行文件(a.out从GCC的main.c)debuggger其10万左右的打印我executed.When使用-static选项,它给了10681的指令的指令作为输入提供给我的考验。
现在2)我创建一个汇编程序,并使用NASM编译和链接,然后当这个可执行文件是作为测试调试器输入它显示8条指令作为计数和容易。
)的程序1执行的指令数很高,因为连接与程序的系统库中的在运行时?用过-static并且由1 / 10.How的因子降低了计数我可以确保指令计数仅仅是主功能的程序1)和其如何计划2)针对调试器报告
1)
的#include<&stdio.h中GT;诠释的main()
{
的printf(你好,世界\\ n!);
返回0;
}
我使用gcc创建可执行文件。
2)
; 64位的Hello World!在Linux中NASM全球_start;全球切入点出口为LD.text段
_开始: ; SYS_WRITE(标准输出,消息长度) MOV RAX,1; SYS_WRITE
MOV RDI,1;标准输出
MOV RSI,消息;消息地址
MOV RDX,长度;消息字符串长度
系统调用 ; sys_exit(return_ code) MOV RAX,60; sys_exit
MOV RDI,0;返回0(成功)
系统调用段.data
消息:DB你好,世界!,0x0A的;消息和换行符
长度:EQU $ -message; NASM定义伪
NASM -f ELF64 -o main.o中-s main.asm中结果
LD -o主要main.o中
)的程序1执行的指令数很高,因为连接与程序的系统库中的在运行时?
块引用>是的,动态链接加上CRT(C运行时)启动文件。
使用
-static
并减少了1/10倍的计数。
块引用>让刚离开CRT调用之前启动文件,其中做的东西
主
,并经过。
我怎样才能确保指令计数只在程序1)`
的主要功能
块引用>衡量一个空
主
,然后减去从未来的测量这个数字。除非你的指令专柜更聪明,并在可执行它的追查过程看符号,它将无法判断哪些code从那里来的。
和它是如何计划2)为调试报告。
块引用>这是因为有是的任何其他$ C $在该程序下进行。这并不是说你不知何故帮助调试程序忽略了一些指令,那就是你犯了一个程序,没有任何说明你没有把自己的存在。
如果你想看到什么确实的发生在你运行GCC的输出,
GDB的a.out
,B _start
,研究
,和单步。一旦你调用树深,你的概率。将要使用鳍
来完成当前函数的执行,因为你不通过字面百万指令,甚至10K想单步。I have a simple debugger(using ptrace : http://pastebin.com/D0um3bUi) to count the number of instructions executed for a given input executable program.It uses ptrace single step execution mode to count instructions.
For that when the program 1)'s executable(a.out from gcc main.c) is given as input to my test debuggger it prints around 100k as instructions executed.When I use -static option it gives 10681 instructions.
Now in 2) I create an assembly program and use nasm for compiling and linking and then when this executable is given as test debuggers input it is showing 8 instructions as the count and which is apt.
The number of instructions executed in program 1) is high because of linking the program with system library's at runtime ? used -static and which reduces the count by a factor of 1/10.How can I ensure that the instruction count is only that of the main function in Program 1) and which is how Program 2) is reporting for the debugger.
1)
#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
I use gcc to create the executable.
2)
; 64-bit "Hello World!" in Linux NASM global _start ; global entry point export for ld section .text _start: ; sys_write(stdout, message, length) mov rax, 1 ; sys_write mov rdi, 1 ; stdout mov rsi, message ; message address mov rdx, length ; message string length syscall ; sys_exit(return_code) mov rax, 60 ; sys_exit mov rdi, 0 ; return 0 (success) syscall section .data message: db 'Hello, world!',0x0A ; message and newline length: equ $-message ; NASM definition pseudo-
nasm -f elf64 -o main.o -s main.asm
ld -o main main.o解决方案The number of instructions executed in program 1) is high because of linking the program with system library's at runtime?
Yep, dynamic linking plus CRT (C runtime) startup files.
used
-static
and which reduces the count by a factor of 1/10.So that just left the CRT start files, which do stuff before calling
main
, and after.How can I ensure that the instruction count is only that of the main function in Program 1)`
Measure an empty
main
, then subtract that number from future measurements.Unless your instruction-counters is smarter, and looks at symbols in the executable for the process it's tracing, it won't be able to tell which code came from where.
and which is how Program 2) is reporting for the debugger.
That's because there is no other code in that program. It's not that you somehow helped the debugger ignore some instructions, it's that you made a program without any instructions you didn't put there yourself.
If you want to see what actually happens when you run the gcc output,
gdb a.out
,b _start
,r
, and single-step. Once you get deep in the call tree, you're prob. going to want to usefin
to finish execution of the current function, since you don't want to single-step through literally 1 million instructions, or even 10k.这篇关于执行的指令数为Hello World程序NASM汇编和C不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!