【问题标题】:BMI Calculator in Assembly装配体中的 BMI 计算器
【发布时间】:2016-02-20 02:36:53
【问题描述】:

我正在尝试在 Assembly 中构建一个 BMI 计算器。我一开始就很迷茫,所以如果这一切都没有任何意义,我提前道歉。我使用预定义的值来制作它,所以我什至不需要请求用户输入。

我使用的是 uVision5,旧设备是 NXP,LPC2104。如果有帮助,我认为这是 ARM。

这是我目前所拥有的,我正在尝试做这个等式:

BMI = (weightInPounds * conversionFactor) / (heightInInches * heightInInches)

规则是我必须有以下数据定义:

  • weightInPounds DCD 150
  • heightInInches DCD 64
  • 转换因子 EQU 703

这是我目前所拥有的:


                    AREA File1, CODE, READONLY
                ENTRY

                LDR r1,weightInPounds   ;loads weight into r1
                LDR r2,heightInInches   ;loads height into r2
                LDR r3,=conversionFactor ;loads conversion into r3
                MUL r4,r1,r3            ;mult weight by conv factor 
                MUL r5,r2,r2            ;square height
                MOV r0,r5, LSR #r4      ;divide previous 2 and store in r0


stop B stop ;force infinite loop by branching to this line

weightInPounds      DCD   150  ;defines weight
heightInInches      DCD   64   ;defines height
conversionFactor    EQU   703  ;defines conversion factor for BMI calc
 END ;end of program

这是我的问题

  • 行 LDR r3,conversionFactor ; 将转换加载到 r3 不起作用,因为 EQU 与 DCD 不同,但我不知道如何修复它,我到处寻找,无法弄清楚.
  • 由于前面提到的错误,第一行 MUL 不起作用。

  • 我不知道如何用寄存器除...

任何帮助将不胜感激。

【问题讨论】:

标签: assembly arm division


【解决方案1】:

查看在 ARM 汇编中实现的用于将 R1 除以 R2 的除法算法:

CMP             R2, #0
BEQ stop
;check for divide by zero!

MOV             R0,#0         
MOV             R3,#1         
;clear R0 to accumulate result
;set bit 0 in R3, which will be 
;shifted left then right

start
    CMP      R2,R1
    MOVLS    R2,R2,LSL#1
    MOVLS    R3,R3,LSL#1
    BLS      start
;shift R2 left until it is about to
;be bigger than R1
;shift R3 left in parallel in order
;to flag how far we have to go

next
    CMP       R1,R2           ;carry set if R1>R2 (don't ask why)
    SUBCS     R1,R1,R2        ;subtract R2 from R1 if this would
                              ;give a positive answer
    ADDCS     R0,R0,R3        ;and add the current bit in R3 to
                              ;the accumulating answer in R0
    MOVS      R3,R3,LSR#1     ;Shift R3 right into carry flag
    MOVCC     R2,R2,LSR#1     ;and if bit 0 of R3 was zero, also
                              ;shift R2 right
    BCC       next            ;If carry not clear, R3 has shifted
                              ;back to where it started, and we
                              ;can end

stop B stop                   ;exit routine

原文链接:https://www.virag.si/2010/02/simple-division-algorithm-for-arm-assembler/

【讨论】:

    【解决方案2】:

    如何划分寄存器的答案是这样的

    DIV destination-register, nominator-register, denominator-register
    

    我可以混合我的提名者和分母,所以检查结果。

    如果可能,您应该避免使用 MOV,因为它在加载常量时只能对 ONE 寄存器的前 8 位进行操作,或者它可以将两个完整的寄存器作为操作数并且不会发生任何异常情况。这是由于arm中的指令格式。原因是源地址有 8 位可用,目标地址有 8 位可用,当其中一个是数字时,它只能占用 8 位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多