简体   繁体   English

AVR,大会,标志 V

[英]AVR, Assembly, Flag V

I need advice on an assembler program (AVR ATMega169).我需要关于汇编程序(AVR ATMega169)的建议。 I have to create code to calculate this expression.我必须创建代码来计算这个表达式。

R20 = (4 * R16 + 3 * R17 - R18) / 8

I try to calculate the arithmetic expression in the code and always check the overflow instruction but it doesn't work properly我尝试计算代码中的算术表达式并始终检查溢出指令但它无法正常工作

  .org 0
start:
    ldi r16, 10
    ldi r17, 20
    ldi r18, 10
        
    lsl r16
    brvs OVERFLOW

    lsl r16
    brvs OVERFLOW

    mov r20,r17
    brvs OVERFLOW

    lsl r20
    brvs OVERFLOW

    add r20,r17
    brvs OVERFLOW

    add r20, r16
    brvs OVERFLOW

    sub r20,r18
    brvs OVERFLOW

    asr r20
    brvs OVERFLOW

    asr r20
    brvs OVERFLOW

    asr r20
    brvs OVERFLOW
    
    
    brvc OK

    OVERFLOW:
    ldi r30, 0x11

    OK:
    ldi r30, 0x11
konec:
    rjmp konec

As far as properly setting the overflow flag, most of the individual operations will set the overflow flag properly, namely add , sub , and lsl .至于正确设置溢出标志,大多数单个操作都会正确设置溢出标志,即addsublsl How ever, asr does not appear to set the overflow flag in a particularly meaningful way regarding signed division.然而, asr似乎并没有以一种特别有意义的方式设置溢出标志,用于有符号除法。

The problematic thing is that the instructions that do properly set the overflow flag also clear the overflow flag (on success / no overflow), so they either set it or clear it based on that one individual arithmetic operation.有问题的是,正确设置溢出标志的指令也会清除溢出标志(成功/没有溢出),所以它们要么设置它,要么根据那个单独的算术运算清除它。 Thus, there appears no direct way to accumulate the overflow condition across multiple instructions.因此,似乎没有直接的方法来累积跨多条指令的溢出条件。

The solution is to test for overflow after each operation, perhaps by branching to the exit (skipping past the rest of the calculation — leaving garbage as in R20 though with the overflow flag set.) Since dividing by 8 cannot overflow, then if you reach that part of the code (ie without having overflowed prior calculations), then I would just clear the overflow flag after the division and before reaching the exit.解决方案是在每次操作后测试溢出,可能通过分支到出口(跳过计算的 rest - 尽管设置了溢出标志,但仍将垃圾留在R20中。)因为除以 8 不会溢出,那么如果你达到那部分代码(即没有溢出先前的计算),那么我将在除法之后和到达出口之前清除溢出标志。

Another approach would be to or the overflow flag after each operation into some variable, then use that variable to set the overflow flag at the end.另一种方法是将每次操作后的溢出标志or溢出标志放入某个变量,然后使用该变量在最后设置溢出标志。 This approach would leave a more predictable value in R20 when overflow does occur, at the expense of additional instructions and variables.当确实发生溢出时,这种方法会在R20中留下更可预测的值,但会牺牲额外的指令和变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM