2014-10-26 52 views
1

我一直在Unix操作系统下编程硬件(鼠标,键盘等),Minix和我遇到一些问题时提示将它与程序集(AT & T语法)相结合。ASM/C中的中断处理程序 - 如何? -Minix

截至目前,我正在对键盘进行编程(获取和打印扫描代码),并且我需要在C和ASM中执行此操作。我已经开发了C函数,它正在正常工作,现在我必须用ASM函数替换IH(中断处理程序),如以下代码所示:(要替换的代码位于函数“receiver_loop”中)

int timer_subscribe_int(void) { 

    int temp = hook_id; //integer between 0 and 31 
    sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE,&hook_id); // returns a hook id that you can then use to enable and disable irqs. 
    sys_irqenable(&hook_id); 
    return temp; 
} 

int timer_unsubscribe_int() { 

    if (sys_irqrmpolicy(&hook_id)!= OK) return 1; 
    return 0; 
} 


void receiver_loop() { 

    int ipc_status,r, seconds = 0, running = 1; 
    message msg; 

    int shift = keyboard_subscribe_int(); 
    int shift_timer; 
    if(timer_flag) shift_timer = timer_subscribe_int(); 

     while(running && (seconds < time)) { 
      /* Get a request message. */ 
      if (driver_receive(ANY, &msg, &ipc_status) != 0) { 
       printf("driver_receive failed with: %d", r); 
       continue; 
      } 
      if (is_ipc_notify(ipc_status)) { /* received notification */ 
       switch (_ENDPOINT_P(msg.m_source)) { 
       case HARDWARE: /* hardware interrupt notification */ 
        if (msg.NOTIFY_ARG & BIT(shift)) { /* subscribed interrupt bit 1 fica a 1, logo é 1*/ 

        // Handle interruption 
        /*Replace the following commented code with ASM function(s) 

         if(keyboard_int_handler()) running = 0; 
         else seconds = 0; 

        } 

        else if (msg.NOTIFY_ARG & BIT(shift_timer) && timer_flag) { // subscribed interrupt bit 1 fica a 1, logo é 1 
         //printf("\n Entrou aqui. Counter %d \n", counter); 
         timer_int_handler(); 
         if (counter%60 == 0){ 
          //as the frequency of interruptions is 60Hz as assumed, that means every 60 interrupts 1 second has passed 
          //so whatever is inside this if-block happens each second 
          seconds++; // increments the seconds counter 
          //printf("\n Segundos: %d \n", seconds); 
        };*/ 
       } 
       break; 
      default: 
       break; /* no other notifications expected: do nothing */ 
      } 
     } else { /* received a standard message, not a notification */ 
      /* no standard messages expected: do nothing */ 
     } 
    } 

    if(seconds >= time) { 
     printf("\nTimeout. Terminating...\n"); 
    } 


    keyboard_unsubscribe_int(); 
    timer_unsubscribe_int(); 
    return; 
} 

这是我得到了什么,到目前为止,说实话,我有点停留在如何从这里走,并得到了类似的ASM功能,以取代注释代码。任何人都可以帮我一把吗?

+0

你的目标CPU是什么?什么操作系统? – nrz 2014-10-26 18:03:57

+0

Minix 3.1.8。你对第一个问题的意思是? – Khabz 2014-10-26 18:08:29

+0

你正在使用什么处理器? – nrz 2014-10-26 18:15:08

回答

3

gcc提供了一个简单的方法来获取您的c代码并输出相关的汇编程序。所以如果你需要把一些C代码转换成汇编器。一个简单的方法看起来像这样:

  1. 首先将代码本地化为一个例程。
  2. __attribute__((noinline))标记此例程以防止gcc 将您的例程内联到其他例程中。虽然通常内联是一件好事,它可以让你的代码更快运行,但如果你试图隔离特定的代码,那不是你所需要的。
  3. 使用如下命令行编译代码:gcc -S -o test.s test.c。请注意,您可以(也可能应该)打开 优化(gcc -O2 -S -o test.s test.c)。
  4. test.c程序集现在正在测试中。检查它找到你的例行程序 。

请注意,默认情况下,i386上的输出将采用att格式。如果您更喜欢英特尔格式,则可以使用-masm=intel