add a new system call for x86

 

#define T_SYSCALL       64      // system call

#define SYS_fork    1
#define SYS_sleep   2

#define SYSCALL(name) \
  .globl name; \
  name: \
    movl $SYS_ ## name, %eax; \
    int $T_SYSCALL; \
    ret


# in kernel,
code to process int $T_SYSCALL (x86's trap machanism),
get system call num from %eax,
get arguments from stack since when arguments are pushed to stack
when system call get called(assume no registered is used for arguments
which is true for x86, but x86_64 use register for arguments).

# usage
int main()
{
   sleep(2);
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是x86汇编代码: ``` section .data msg db 'Enter an integer between 0 and 20: $' summsg db 'The sum of cubes from 0 to %d is %d$' nl db 0dh, 0ah, '$' x db ? sum dw 0 section .text global _start _start: ; print message and read input mov eax, 4 ; system call for write mov ebx, 1 ; file descriptor for stdout mov ecx, msg mov edx, 31 ; message length int 0x80 mov eax, 3 ; system call for read mov ebx, 0 ; file descriptor for stdin mov ecx, x mov edx, 1 ; number of bytes to read int 0x80 sub byte [x], '0' ; convert character to number ; calculate sum of cubes xor ecx, ecx ; clear ecx mov ax, 0 ; clear ax loop1: cmp cl, [x] jg done push ecx ; save ecx on stack mov ebx, ecx mov ax, cx ; calculate cube of cx mul cx mul bx add [sum], ax ; add cube to sum pop ecx ; restore ecx inc cl ; increment ecx jmp loop1 done: ; print sum of cubes mov eax, 4 ; system call for write mov ebx, 1 ; file descriptor for stdout mov ecx, nl mov edx, 2 ; message length int 0x80 mov eax, 4 ; system call for write mov ebx, 1 ; file descriptor for stdout mov ecx, summsg mov edx, 31 ; message length int 0x80 mov eax, [sum] ; move sum to eax mov ebx, 10 ; set ebx to 10 xor ecx, ecx ; clear ecx xor edx, edx ; clear edx loop2: div ebx ; divide eax by 10 add edx, '0' ; convert remainder to character push edx ; push remainder onto stack inc cl ; increment ecx cmp eax, 0 ; check if quotient is zero jne loop2 print: pop edx ; pop remainder from stack mov eax, 4 ; system call for write mov ebx, 1 ; file descriptor for stdout mov ecx, edx mov edx, 1 ; message length int 0x80 loop print ; loop until stack is empty mov eax, 1 ; system call for exit xor ebx, ebx ; exit status int 0x80 ``` 这个程序与之前的程序非常相似,只不过使用了x86汇编语言,并使用了Linux系统调用来进行输入和输出。请注意,Linux系统调用使用`int 0x80`指令来触发,而不是`int 21h`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值