AVR,组件,标志V [英] AVR, Assembly, Flag V

查看:58
本文介绍了AVR,组件,标志V的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关汇编程序的建议(AVR ATMega169).我必须创建代码来计算此表达式.

I need advice on an assembler program (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

推荐答案

就正确设置溢出标志而言,大多数单个操作都会正确设置溢出标志,即 add sub lsl .但是,对于有符号除法, asr 似乎没有以特别有意义的方式设置溢出标志.

As far as properly setting the overflow flag, most of the individual operations will set the overflow flag properly, namely add, sub, and lsl.  How ever, asr does not appear to set the overflow flag in a particularly meaningful way regarding signed division.

有问题的是,正确设置溢出标志的指令也会清除溢出标志(成功/没有溢出),因此它们要么基于一个单独的算术运算就对其进行设置或清除.因此,似乎没有直接的方法可以在多个指令之间累加溢出条件.

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.

解决方案是在每次操作后测试溢出,也许是通过分支到出口(跳过其余的计算,尽管设置了溢出标志,跳过了 R20 中的垃圾操作).除以8不会溢出,然后,如果到达代码的那部分(即,没有使先前的计算溢出),那么我只需要在除法之后和到达出口之前清除溢出标志即可.

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 (i.e. without having overflowed prior calculations), then I would just clear the overflow flag after the division and before reaching the exit.

另一种方法是在每次操作后将溢出标志或成某个变量,然后使用该变量在最后设置溢出标志.当确实发生溢出时,此方法将在 R20 中留下更可预测的值,但会增加其他指令和变量.

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.  This approach would leave a more predictable value in R20 when overflow does occur, at the expense of additional instructions and variables.

这篇关于AVR,组件,标志V的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆