使用谓词指令将 c 转换为汇编 [英] convert c to assembly with predicated instruction

查看:30
本文介绍了使用谓词指令将 c 转换为汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用谓词指令将此代码转换为汇编

I want to convert this code to assembly using predicated instruction

If (A>B){

    C=A;
    D=B;
    E=0

}

else{

    C=B;
} 

是否正确或者我如何使用跳转?

Is it correct or how can i use jump ?

cmp R1,R2; considering B is assigned to R2 and A assigned to R1
movlf R3,R1;R3 assign to C
mov R4,R2;R4 assign to D
mov R5,0; R5 assign to E
movlt R3,R2

推荐答案

警告:为新手解答.可能会让有经验的用户厌烦死.

WARNING: Answer for newbies. May bore experienced users to death.

我不确定您是否误用了术语,或者您是否真的想使用预测说明1.

I'm not sure if you misused the terminology or if you really want to use predicated instructions1.

在后一种情况下,使用 ARM v6 predication 作为一个研究案例(并继承您关于寄存器使用的前提),程序集很简单

In the latter case, using ARM v6 predication as a study case (and inheriting your premises about register usage), the assembly is simply

;r1 = A    r2 = B    r3 = C    r4 = D    r5 = E
;
;A, B unsigned            | ;A, B signed
                          |
cmp r1, r2                | cmp r1, r2 
                          |
movhi r3, r1              | movgt r3, r1
movhi r4, r2              | movgt r4, r2
movhi r5, #0              | movgt r5, #0
                          |
movls r3, r2              | movle r3, r2

这里我根据涉及的变量的符号给出了两个版本.

Here I gave two versions based on the sign of the involved variables.

movhi 表示如果更高就移动.movls 表示如果低于或相同则移动.
movgt 表示如果更大则移动.movle 表示小于或等于移动.
它们意味着相同的算术比较,只是后者对有符号数使用了正确的标志.

movhi means move if higher. movls means move if lower or same.
movgt means move if greater. movle means move if less or equal.
They mean the same arithmetic comparison, it is just that the latter uses the proper flags for signed numbers.

我对指令进行了分组,因此很容易识别 if-thenelse 块.
请注意同一块中的指令如何具有相同的后缀(例如 hils).

I grouped the instructions, so it is easy to identify the if-then and the else blocks.
Note how instructions in the same block have the same suffix (e.g. hi and ls).

真正使这段代码成为 if-then-else 结构而不是其他东西的原因是条件 hi-lsgt-le互斥的(两者中只有一个为真).
所以只能执行一个指令块.

What really makes this code a if-then-else construct rather than something else is the fact that the conditions hi-ls and gt-le are mutually exclusive (only one of the twos can be true).
So only one block of instructions can be executed.

使用非互斥条件会导致多个if-then-else 语句.

Using non mutually exclusive conditions give rise to multiple if-then-else statements.

如果你误用了这个术语,而你实际上只是想实现一个条件语句(或选择),即一个if-then-else,那么通常的方法是一个条件分支2,正如 Nutan 已经展示的那样.
这里有一个更易读的版本:

If you misused the terminology and you actually wanted just to implement a conditional statement (or selection), i.e. an if-then-else, then the usual approach is a conditional branch2 as Nutan already shown.
Here a slightly more readable version:

 cmp r1, r2
 bls _A_less_same_B

 mov r3, r1
 mov r4, r2
 eor r5, r5, r5

b _end_if

_A_less_same_B:
 mov r3, r2

_end_if:

转换此代码以使用有符号整数的负担由您决定.

Up to you the burden to convert this code to work with signed integers.

以冒号 (:) 结尾的花哨词称为 labels,它们是在代码(和数据)中命名点的有用方法3.
将其视为灵活的行号.

The fancy words ending with a colon (:) are called labels, they are an useful way to name points in code (and data)3.
Think about that as flexible line numbers.

b 表示 branch,一旦执行,下一条指令将从指定为操作数的标签(地址)中获取(例如从 _end_if).
bls 只是一个谓词 b(bls 的意思是分支如果更少或相同),俗称条件分支.

b means branch, once it is executed the next instruction is fetched from the label (address) specified as operand (for example from _end_if).
bls is just a predicated b (bls means branch if less or same), commonly known as conditional branch.

条件分支就像普通分支,但如果指定的条件不满足,它们可以被忽略".
如果条件满足并且 CPU 执行跳转,从而从指定为操作数的标签中获取下一条指令,则称为进行条件跳转.
如果条件不满足并且CPU从分支之后的指令继续执行(程序流程失败em>).

Conditional branches are just like normal branches but they can be "ignored" if the conditions specified fails to meet.
A conditional jump is said to be taken if the conditions are met and the CPU execute the jump, thereby fetching the next instructions from the label specified as operand.
It is said to be not taken if the conditions are not met and the CPU continue the execution from the instruction after the branch (the program flow fall through).

条件"通常意味着设置和清除标志.一些指令,如 cmp,设置和清除这些标志.
其他指令,如 bls 使用这些标志.

The "conditions" usually means flags set and cleared. Some instruction, like cmp, set and clear these flags.
Other instructions, like bls use these flags.

标志保存在专用寄存器中(ARM 中的 ps),但有些体系结构,尤其是 MIPS,没有标志寄存器.

The flags are hold in a dedicated registers (ps in ARM) but there are architectures, most notably MIPS, which don't have a flags register.

您可以用手指来模拟程序流程.例如,如果 A >B 流程如下:

You can use your finger to simulate the program flow. For example if A > B the flow is as follow:

                            [Start Here]
                             ¯¯¯¯+¯¯¯¯¯
 cmp r1, r2                      |
 bls _A_less_same_B              + [Branch not taken, fall through]
                                 |
 mov r3, r1                      |
 mov r4, r2                      |
 eor r5, r5, r5                  |
                                 |
b _end_if                        +--[Branch always taken]----+
                                                             |
_A_less_same_B:                                              |
 mov r3, r2                                                  |
                                                             |
_end_if:                         +--[Land here]--------------+
                                 |
                                 V

弯曲意味着跳过"我们想要跳过的代码(在本例中为else).

The bend is mean to picture a "jump over" the code that we want to skip (the else in this case).

我不认识你的问题的汇编风格,所以我无法帮助写具体的例子.
反正我不会做,因为我觉得这个一般性的解释就足够了,希望我这边的这种缺乏努力会促使你尝试自己解决这个练习.

I don't recognize the assembly flavor of your question, so I cannot help with writing concrete examples.
I wouldn't do it anyway as I feel that this general explanation is enough and in the hope that such lack of effort on my side will spur you into attempting solving the exercise by yourself.

这是学习路线上的必修步骤.

Which is a mandatory step on the route of learning.

1 获取、解码(可能也发出)但仅在设置或清除特定标志时才执行的指令.

1 Instruction that are fetched, decoded, (probably issued too) but only executed if particular flags are set or cleared.

2 请注意,如果可能,最好避免条件分支.取决于目标微架构,可以有一种更优化的方式来实现相同的结果.这只是值得注意的,现在不要打扰.

2 Note that conditional branches are best avoided if possible. Depending on the targeting micro-architecture there can be a more optimal way to achieve the same result. This was just worth noting, don't bother with it right now.

3 实际上是将成为地址的偏移量.

3 Actually offsets that will become addresses.

这篇关于使用谓词指令将 c 转换为汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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