6502 汇编二进制到 bcd - 在 x86 上可能吗?

如何解决6502 汇编二进制到 bcd - 在 x86 上可能吗?

我有几个关于此代码的问题:

; Convert an 16 bit binary value to BCD
;
; This function converts a 16 bit binary value into a 24 bit BCD. It
; works by transferring one bit a time from the source and adding it
; into a BCD value that is being doubled on each iteration. As all the
; arithmetic is being done in BCD the result is a binary to decimal
; conversion. All conversions take 915 clock cycles.
;
; See BINBCD8 for more details of its operation.
;
; Andrew Jacobs,28-Feb-2004

        .ORG $0200

BINBCD16:   SED     ; Switch to decimal mode
        LDA #0      ; Ensure the result is clear
        STA BCD+0
        STA BCD+1
        STA BCD+2
        LDX #16     ; The number of source bits

CNVBIT:     ASL BIN+0   ; Shift out one bit
        ROL BIN+1
        LDA BCD+0   ; And add into result
        ADC BCD+0
        STA BCD+0
        LDA BCD+1   ; propagating any carry
        ADC BCD+1
        STA BCD+1
        LDA BCD+2   ; ... thru whole result
        ADC BCD+2
        STA BCD+2
        DEX     ; And repeat for next bit
        BNE CNVBIT
        CLD     ; Back to binary

        BRK     ; All Done.

; A test value to be converted

        .ORG $0300

BIN     .DW  12345
BCD     .DS  3

来自 this 网站。

我不明白这一行到底是做什么的:

ROL BIN+1

它是否对 BIN 的第二个字节执行右移?如果是这样,这个字节到底是什么?

还有可能为 x86 编写类似的东西吗?是否可以使用 BCD 以某种优雅的方式使用 x86 以十进制打印数字?还是最好坚持除以 10?我对 AAA、AAM 指令有所了解,但我不知道它们是否真的有用。

解决方法

ROL = 向左旋转。是的,这是第二个字节。 ASL+ROL 一起将 BINBIN+1 中的 16 位数字左移一位。 ROL 用于将低字节的 MSB 传播到高字节的 LSB,而将高字节的 MSB 移动到 ADC 指令使用的进位标志。>

请注意,此代码使用打包的 BCD,因此在 x86 上,您需要使用 DAA 而不是 AAA 指令。此外,BCD 内容已被弃用,并且在 64 位模式下不可用。然而,这里是等效的 x86 代码,添加了文本转换和打印。 GNU 汇编器 at&t,32 位 linux:

.globl main
main:
    sub $8,%esp
    mov $12345,%edx
    mov $16,%ecx
repeat:
    shl %dx
    mov bcd,%al
    adc %al,%al
    daa
    mov %al,bcd
    mov bcd+1,bcd+1
    mov bcd+2,bcd+2
    dec %ecx
    jnz repeat

# print
    lea bcd+2,%esi
    lea txt,%edi
    call unpack
    call unpack
    call unpack
    push $txt
    call puts
    call exit

unpack:
    mov (%esi),%al
    dec %esi
    mov %al,%ah
    shr $4,%al
    and $15,%ah
    add $0x3030,%ax
    stosw
    ret

.lcomm bcd,3
.lcomm txt,7

以上不是推荐的通用 int-to-string 方法,它只是问题中 6502 代码的翻译。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?