ASM / C中的中断处理程序 - 如何? -Minix

时间:2014-10-26 18:01:57

标签: c assembly keyboard interrupt-handling minix

我一直在Unix操作系统中编写硬件(鼠标,键盘等),Minix和我在提示将它与Assembly(AT& T语法)结合时遇到了一些问题。

截至目前,我正在编程键盘(获取并打印扫描码),我需要在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功能来替换注释代码。任何人都可以帮我一把吗?

1 个答案:

答案 0 :(得分: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的程序集现在处于test.s.检查它找到你的 例程。
  5. 请注意,默认情况下,i386上的输出将采用att格式。如果您更喜欢英特尔格式,可以使用-masm=intel