[英]6502 assembly binary to bcd - is that possible on 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
从这个网站。
我不明白这条线到底是做什么的:
ROL BIN+1
它是否对 BIN 的第二个字节执行右移? 如果是这样,这个字节到底是什么?
也可以为 x86 写类似的东西吗? 是否可以使用 BCD 以某种优雅的方式使用 x86 打印十进制数字? 还是最好坚持除以 10? 我对 AAA、AAM 指令有所了解,但我不知道它们是否真的有用。
ROL
= 向左旋转。 是的,这是第二个字节。 ASL
+ ROL
一起将BIN
和BIN+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, %al
adc %al, %al
daa
mov %al, bcd+1
mov bcd+2, %al
adc %al, %al
daa
mov %al, 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 到字符串的推荐方法,它只是问题中 6502 代码的翻译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.