关于AT&T汇编与系统调用的问题

lane_L 2014-11-08 09:00:46
1.这是一个作业.
2.这是题目:
Writing an assembly program, like 'time', to record the executing time of other
process (usually its child process, use fork to create). 'int 0x80' is recommended to use
the system call.
$ mytimer ls -lR
1m2.560s
Requirement:
1. The program should run under Linux, BSD, UNIX etc. POSIX-based
operating systems.
2. AT&T assembly language style is required. ( different from Intel assembly
language style )
3. Don't use command, eg. 'time' 'date', etc., directly in the program by
system().
3.不是求作业答案的,只要给一个系统调用参数是struct的汇编调用例子.
4.我的想法是用gettimeofday完成.
5.这是我用C试写的
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>

int main(){
struct timeval start,end;
gettimeofday(&start,NULL);
char str[50];
scanf("%[^\n]",str);
system(str);
gettimeofday(&end,NULL);
double time = end.tv_sec-start.tv_sec+(end.tv_usec-start.tv_usec)/1000000.0;
printf("time:%.3f\n",time);
return 0;
}
谢谢
...全文
92 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2014-11-17
  • 打赏
  • 举报
回复
建议楼主做两件事情 1. 阅读time命令的源码 2. 了解AT&T汇编
Cody2k3 2014-11-10
  • 打赏
  • 举报
回复
引用 楼主 u010592415 的回复:
1.这是一个作业. 2.这是题目: Writing an assembly program, like 'time', to record the executing time of other process (usually its child process, use fork to create). 'int 0x80' is recommended to use the system call. $ mytimer ls -lR 1m2.560s Requirement: 1. The program should run under Linux, BSD, UNIX etc. POSIX-based operating systems. 2. AT&T assembly language style is required. ( different from Intel assembly language style ) 3. Don't use command, eg. 'time' 'date', etc., directly in the program by system(). 3.不是求作业答案的,只要给一个系统调用参数是struct的汇编调用例子. 4.我的想法是用gettimeofday完成. 5.这是我用C试写的 #include <sys/time.h> #include <stdio.h> #include <unistd.h> int main(){ struct timeval start,end; gettimeofday(&start,NULL); char str[50]; scanf("%[^\n]",str); system(str); gettimeofday(&end,NULL); double time = end.tv_sec-start.tv_sec+(end.tv_usec-start.tv_usec)/1000000.0; printf("time:%.3f\n",time); return 0; } 谢谢
简单用gas给楼主凑合一个供参考(所以没有漂亮的struct 宏) 1 .data 2 .globl hello 3 hello: 4 .string "Hi World\n" 5 begin: 6 .int 0 7 .int 0 8 end: 9 .int 0 10 .int 0 11 12 .text 13 .global main 14 main: 15 push %ebp 16 movl %esp, %ebp 17 18 push $hello 19 call puts 20 21 movl $78, %eax 22 movl $begin, %ebx 23 movl $0, %ecx 24 int $0x80 25 26 push $1 27 call sleep 28 29 movl $78, %eax 30 movl $end, %ebx 31 movl $0, %ecx 32 int $0x80 33 34 leave 35 ret $gcc -m32 -o hello hello.s $./hello Hi World 因为是64位系统所以用m32说明目标是32位程序

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧