Assembly 汇编中的BMI计算器

Assembly 汇编中的BMI计算器,assembly,arm,division,Assembly,Arm,Division,我正试图在汇编中构建一个体重指数计算器。我一开始就迷路了,所以如果这一切毫无意义,我会提前道歉。我使用预定义的值来创建它,所以我甚至不需要请求用户输入 我使用的是uVision5,传统设备是NXP,LPC2104。如果有帮助的话,我想这就是我的手臂 这是我到目前为止得到的,我正试图做这个等式: BMI=(加权磅数*换算系数)/(身高英寸*身高英寸) 规则是我必须有以下数据定义: weightInPounds DCD 150 高度英寸DCD 64 换算系数EQU 703 以下是我到目前为止的情

我正试图在汇编中构建一个体重指数计算器。我一开始就迷路了,所以如果这一切毫无意义,我会提前道歉。我使用预定义的值来创建它,所以我甚至不需要请求用户输入

我使用的是uVision5,传统设备是NXP,LPC2104。如果有帮助的话,我想这就是我的手臂

这是我到目前为止得到的,我正试图做这个等式:

BMI=(加权磅数*换算系数)/(身高英寸*身高英寸)

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

  • weightInPounds DCD 150
  • 高度英寸DCD 64
  • 换算系数EQU 703
以下是我到目前为止的情况:



这是我的问题

  • 线LDR r3,换算系数;加载到r3的转换不起作用,因为EQ与DCD不同,但我不知道如何修复它,而且我到处都找过,也找不到它
  • 由于前面提到的错误,第一条MUL线无法工作

  • 我不知道如何除以寄存器。。。在“MOV r0,r5,LSR#r4”这行中,我要做的是像方程中那样除法


任何帮助都将不胜感激。

如何划分寄存器的答案如下

DIV destination-register, nominator-register, denominator-register
我可以把我的提名人和分母混在一起,所以检查结果


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

请查看ARM assembly中用于将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

原始链接:

ARM乘法和除法指令:@ScottHunter非常感谢您的回复,当我实现“UDIV”或“SDIV”时,我收到错误“A1854E:未知操作码“SDIV”,可能是错误的目标CPU?”。LPC2104有一个ARM7TDMI内核;您没有除法指令。我建议实现一个迭代除法子程序(即循环中的减法)-这不是最有效的方法,但肯定是最直接的方法。也可能重复:
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