如何使用ARM200汇编语言打印大于10的数字 [英] How to print numbers greater then 10 in ARM200 Assembly language

查看:286
本文介绍了如何使用ARM200汇编语言打印大于10的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用ARM200学习汇编语言.我有一部分内存用32个整数填充.我需要能够将这32个整数打印到屏幕上.只需将数字0的ASCII值添加到寄存器中,就可以轻松打印出数字0-9,但是我非常困惑如何打印大于9的数字.

We are using ARM200 to learn assembly language. I have a portion of memory with 32 integers filling it. I need to be able to print out these 32 integers to the screen. I can print out numbers 0 - 9 easy enough just by adding the ASCII value of the number 0 to what is in the register, but I'm very confused how you print out numbers greater then 9.

Print   LDR     r5, [r2]        ;load whats in that part of memory to r5.
        CMP     r5, #9      ;compare if number is greater or less then 9
       ADDLE    r0, r5, #"0"    ;add value in array to ascii value of 0 to print
       SWI  SWI_WriteC  ;Print Value    
       ADD  r6, r6, #1  ;increment counter
       ADD  r2, r2, #4  ;move portion of memory to the next int.
       CMP  r6, #32     ;check if you are done printing 32 ints
       BNE  Print       ;if not loop back up to print
       MOV  pc, r14     ;return

r0是用于打印的寄存器,并且r2指向所有整数在内存中的位置. r5是我将内存中的值放入其中的内容,而r6用于计数器.
是的,我确实意识到内存中的每个数字之间有4个字节的空间,但这对这个项目无关紧要.

r0 is the register used for printing and r2 points to the location in memory for all the integers. r5 is what i put the value from memory into and r6 is used for a counter.
Yes i do realize that there is 4 bytes of space in between each number in memory, but this does not matter for this project.

推荐答案

由于您正在学习(即可能的家庭作业),因此,我仅提供一般性建议.

Since you're learning (ie, possible homework), I'll give general advice only.

假设您有数字247,并且想要一一打印出三个数字.

Let's say you have the number 247 and you wanted to print out the three digits of it, one by one.

如何从247中获得百位数2并将47留给下一次迭代?

How can you get the hundreds digit 2 from 247 and leave 47 for the next iteration?

将该值放入一个临时变量中,并将计数器设置为零.然后,当温度值大于99时,将其减去100,然后将1加到计数器上.

Put that value into a temporary variable and set a counter to zero. Then, while the temp value is greater than 99, subtract 100 from it and add 1 to the counter.

这将为您提供一个计数器2和一个临时值47.使用2输出数字(您声明已经可以这样做).

This will give you a counter of 2 and a temp value of 47. Use the 2 to output your digit (you state you can already do this).

现在移至十.

将计数器设置回零.

然后,当温度值大于9时,将其减去10,然后将1加到计数器上.

Then, while the temp value is greater than 9, subtract 10 from it and add 1 to the counter.

这将为您提供一个计数器4和一个临时值7.使用4输出您的数字.

This will give you a counter of 4 and a temp value of 7. Use the 4 to output your digit.

最后,单位.

使用最后的余数7输出最后一位.

Use the final remainder 7 to output the last digit.

这是我在另一个答案(略作修改)中使用的类似于汇编程序的伪代码,以执行类似的操作.

Here's some assembler-like pseudo-code I used in another answer (slightly modified) to do a similar thing.

    val = 247

    units = val
    tens = 0
    hundreds = 0
loop1:
    if units < 100 goto loop2
    units = units - 100
    hundreds = hundreds + 1
    goto loop1
loop2:
    if units < 10 goto done
    units = units - 10
    tens = tens + 1
    goto loop2
done:
    if hundreds > 0:                 # Don't print leading zeroes.
        output hundreds
    if hundreds > 0 or tens > 0:
        output tens
    output units
    ;; hundreds = 2, tens = 4, units = 7.


另一件事,所有这些东西都需要放入子例程中,以便您可以重用它们.将上述算法的32个副本转换为汇编代码将是非常繁琐的代码.


And one other thing, all this stuff needs to go into subroutines so that you can re-use them. Having thirty-two copies of that algorithm above converted to assembly would be a very tedious piece of code.

这篇关于如何使用ARM200汇编语言打印大于10的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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