【问题标题】:what is the equivalent of an if-statement in ARM?什么是 ARM 中的 if 语句?
【发布时间】:2016-10-16 00:55:00
【问题描述】:

所以,我正在 ARM 中开发一个程序,该程序从文件中获取一堆数字并判断它们是偶数还是奇数。

问题是我知道如何乘以 0.5,但我不知道如何在 ARM 中执行这种高级语句

if (A / 2 == 0)
    print even
else
    print odd

以下是我的代码:

@open input file
ldr r0,=FileName        @ set Name for input file
mov r1,#0                 @ mode is input
swi SWI_Open              @ open file for input
bcs InFileError           @ if error?
ldr r1,=InFileHandle      @ load input file handle
str r0,[r1]               @ save the file handle

@read integers from input file
NUMBERS:
ldr r0,=InputFileHandle   @ load input file handle
ldr r0,[r0]
swi SWI_RdInt             @ read the integer into R0
bcs EofReached       @ Check Carry-Bit (C): if= 1 then EOF reached

@multiplication by 0.5 to test for odd or even
MUL R2 R0 0.5
@what is the test in ARM
@for ( R0 / 0.5 ) == a multiple of 1?

B NUMBERS
LOOP:

@end of program
Message1: .asciz"Hello World!"
EOL:     .asciz   "\n"
NewL:    .ascii   "\n"
Blank:   .ascii   " "
FileName: .asciz"input.txt"
.end

所以我认为从文件输入和读取整数的前两件事是可行的。我不知道如何测试它可以被 2 整除的条件。我认为它乘以 0.5,然后下一步是说即使该数字没有小数位,后面还有任何内容,但如果是这样,那么除以数字 B 的数字 A 是奇数。否则是偶数?

【问题讨论】:

    标签: input architecture arm division multiplication


    【解决方案1】:

    一个简短的回答:你不需要乘以 0.5 或类似的东西。您需要检查该值的 LSB(最低有效位)的值。偶数为 0,奇数为 1。

    更新:您的“C”代码也是错误的。您想使用 A % 2,而不是 A / 2

    【讨论】:

    • 谢谢。我知道编程中的内容,我只是忘记了,因为它已经有一段时间了。我知道 % 是模运算符。当我打字的时候我真的很累。
    • 你能给我发一个链接到一篇文章的链接,上面写着如何检查 LSB 的值吗?我想知道我在做什么。你能举个例子吗?
    • 没有文章,是初级操作。如果您正在学习 ARM asm,那么您有 ARM 参考手册,不是吗?如果没有 - 谷歌搜索并下载,这是必须的。查看 TST 指令,或 ANDS R0,R0,#1 - 会做同样的工作,但会破坏 R0 的内容。(PS:您也可以编辑问题以纠正错误)。
    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 2011-01-10
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多