⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convert.asm

📁 convert.asm: 1.From ASCII resp. BCD to binary 2.From binary to ASCII resp. BCD 3.From binary to H
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;
; Package II: From binary to ASCII resp. BCD
;
; Bin2ToAsc5
; ==========
; converts a 16-bit-binary to a 5 digit ASCII-coded decimal
; In: 16-bit-binary in rBin1H:L, Z points to the highest
;   of 5 ASCII digits, where the result goes to
; Out: Z points to the beginning of the ASCII string, lea-
;   ding zeros are filled with blanks
; Used registers: rBin1H:L (content is not changed),
;   rBin2H:L (content is changed), rmp
; Called subroutines: Bin2ToBcd5
;
Bin2ToAsc5:
	rcall Bin2ToBcd5 ; convert binary to BCD
	ldi rmp,4 ; Counter is 4 leading digits
	mov rBin2L,rmp
Bin2ToAsc5a:
	ld rmp,z ; read a BCD digit
	tst rmp ; check if leading zero
	brne Bin2ToAsc5b ; No, found digit >0
	ldi rmp,' ' ; overwrite with blank
	st z+,rmp ; store and set to next position
	dec rBin2L ; decrement counter
	brne Bin2ToAsc5a ; further leading blanks
	ld rmp,z ; Read the last BCD
Bin2ToAsc5b:
	inc rBin2L ; one more char
Bin2ToAsc5c:
	subi rmp,-'0' ; Add ASCII-0
	st z+,rmp ; store and inc pointer
	ld rmp,z ; read next char
	dec rBin2L ; more chars?
	brne Bin2ToAsc5c ; yes, go on
	sbiw ZL,5 ; Pointer to beginning of the BCD
	ret ; done
;
; Bin2ToAsc
; =========
; converts a 16-bit-binary to a 5-digit ASCII coded decimal,
;   the pointer points to the first significant digit of the
;   decimal, returns the number of digits
; In: 16-bit-binary in rBin1H:L, Z points to first digit of
;   the ASCII decimal (requires 5 digits buffer space, even
;   if the number is smaller!)
; Out: Z points to the first significant digit of the ASCII
;   decimal, rBin2L has the number of characters (1..5)
; Used registers: rBin1H:L (unchanged), rBin2H (changed),
;   rBin2L (result, length of number), rmp
; Called subroutines: Bin2ToBcd5, Bin2ToAsc5
;
Bin2ToAsc:
	rcall Bin2ToAsc5 ; Convert binary to ASCII
	ldi rmp,6 ; Counter is 6
	mov rBin2L,rmp
Bin2ToAsca:
	dec rBin2L ; decrement counter
	ld rmp,z+ ; read char and inc pointer
	cpi rmp,' ' ; was a blank?
	breq Bin2ToAsca ; Yes, was a blank
	sbiw ZL,1 ; one char backwards
	ret ; done
;
; Bin2ToBcd5
; ==========
; converts a 16-bit-binary to a 5-digit-BCD
; In: 16-bit-binary in rBin1H:L, Z points to first digit
;   where the result goes to
; Out: 5-digit-BCD, Z points to first BCD-digit
; Used registers: rBin1H:L (unchanged), rBin2H:L (changed),
;   rmp
; Called subroutines: Bin2ToDigit
;
Bin2ToBcd5:
	push rBin1H ; Save number
	push rBin1L
	ldi rmp,HIGH(10000) ; Start with tenthousands
	mov rBin2H,rmp
	ldi rmp,LOW(10000)
	mov rBin2L,rmp
	rcall Bin2ToDigit ; Calculate digit
	ldi rmp,HIGH(1000) ; Next with thousands
	mov rBin2H,rmp
	ldi rmp,LOW(1000)
	mov rBin2L,rmp
	rcall Bin2ToDigit ; Calculate digit
	ldi rmp,HIGH(100) ; Next with hundreds
	mov rBin2H,rmp
	ldi rmp,LOW(100)
	mov rBin2L,rmp
	rcall Bin2ToDigit ; Calculate digit
	ldi rmp,HIGH(10) ; Next with tens
	mov rBin2H,rmp
	ldi rmp,LOW(10)
	mov rBin2L,rmp
	rcall Bin2ToDigit ; Calculate digit
	st z,rBin1L ; Remainder are ones
	sbiw ZL,4 ; Put pointer to first BCD
	pop rBin1L ; Restore original binary
	pop rBin1H
	ret ; and return
;
; Bin2ToDigit
; ===========
; converts one decimal digit by continued subraction of a
;   binary coded decimal
; Used by: Bin2ToBcd5, Bin2ToAsc5, Bin2ToAsc
; In: 16-bit-binary in rBin1H:L, binary coded decimal in
;   rBin2H:L, Z points to current BCD digit
; Out: Result in Z, Z incremented
; Used registers: rBin1H:L (holds remainder of the binary),
;   rBin2H:L (unchanged), rmp
; Called subroutines: -
;
Bin2ToDigit:
	clr rmp ; digit count is zero
Bin2ToDigita:
	cp rBin1H,rBin2H ; Number bigger than decimal?
	brcs Bin2ToDigitc ; MSB smaller than decimal
	brne Bin2ToDigitb ; MSB bigger than decimal
	cp rBin1L,rBin2L ; LSB bigger or equal decimal
	brcs Bin2ToDigitc ; LSB smaller than decimal
Bin2ToDigitb:
	sub rBin1L,rBin2L ; Subtract LSB decimal
	sbc rBin1H,rBin2H ; Subtract MSB decimal
	inc rmp ; Increment digit count
	rjmp Bin2ToDigita ; Next loop
Bin2ToDigitc:
	st z+,rmp ; Save digit and increment
	ret ; done
;
; **************************************************
;
; Package III: From binary to Hex-ASCII
;
; Bin2ToHex4
; ==========
; converts a 16-bit-binary to uppercase Hex-ASCII
; In: 16-bit-binary in rBin1H:L, Z points to first
;   position of the four-character Hex-ASCII
; Out: Z points to the first digit of the four-character
;   Hex-ASCII, ASCII digits A..F in capital letters
; Used registers: rBin1H:L (unchanged), rmp
; Called subroutines: Bin1ToHex2, Bin1ToHex1
;
Bin2ToHex4:
	mov rmp,rBin1H ; load MSB
	rcall Bin1ToHex2 ; convert byte
	mov rmp,rBin1L
	rcall Bin1ToHex2
	sbiw ZL,4 ; Set Z to start
	ret
;
; Bin1ToHex2 converts an 8-bit-binary to uppercase hex
; Called by: Bin2ToHex4
;
Bin1ToHex2:
	push rmp ; Save byte
	swap rmp ; upper to lower nibble
	rcall Bin1ToHex1
	pop rmp ; Restore byte
Bin1ToHex1:
	andi rmp,$0F ; mask upper nibble
	subi rmp,-'0' ; add 0 to convert to ASCII
	cpi rmp,'9'+1 ; A..F?
	brcs Bin1ToHex1a
	subi rmp,-7 ; add 7 for A..F
Bin1ToHex1a:
	st z+,rmp ; store in target
	ret ; and return
;
; *******************************************
;
; Package IV: From Hex-ASCII to binary
;
; Hex4ToBin2
; converts a 4-digit-hex-ascii to a 16-bit-binary
; In: Z points to first digit of a Hex-ASCII-coded number
; Out: T-flag has general result:
;   T=0: rBin1H:L has the 16-bit-binary result, Z points
;     to the first digit of the Hex-ASCII number
;   T=1: illegal character encountered, Z points to the
;     first non-hex-ASCII character
; Used registers: rBin1H:L (result), R0 (restored after
;   use), rmp
; Called subroutines: Hex2ToBin1, Hex1ToBin1
;
Hex4ToBin2:
	clt ; Clear error flag
	rcall Hex2ToBin1 ; convert two digits hex to Byte
	brts Hex4ToBin2a ; Error, go back
	mov rBin1H,rmp ; Byte to result MSB
	rcall Hex2ToBin1 ; next two chars
	brts Hex4ToBin2a ; Error, go back
	mov rBin1L,rmp ; Byte to result LSB
	sbiw ZL,4 ; result ok, go back to start
Hex4ToBin2a:
	ret
;
; Hex2ToBin1 converts 2-digit-hex-ASCII to 8-bit-binary
; Called By: Hex4ToBin2
;
Hex2ToBin1:
	push R0 ; Save register
	rcall Hex1ToBin1 ; Read next char
	brts Hex2ToBin1a ; Error
	swap rmp; To upper nibble
	mov R0,rmp ; interim storage
	rcall Hex1ToBin1 ; Read another char
	brts Hex2ToBin1a ; Error
	or rmp,R0 ; pack the two nibbles together
Hex2ToBin1a:
	pop R0 ; Restore R0
	ret ; and return
;
; Hex1ToBin1 reads one char and converts to binary
;
Hex1ToBin1:
	ld rmp,z+ ; read the char
	subi rmp,'0' ; ASCII to binary
	brcs Hex1ToBin1b ; Error in char
	cpi rmp,10 ; A..F
	brcs Hex1ToBin1c ; not A..F
	cpi rmp,$30 ; small letters?
	brcs Hex1ToBin1a ; No
	subi rmp,$20 ; small to capital letters
Hex1ToBin1a:
	subi rmp,7 ; A..F
	cpi rmp,10 ; A..F?
	brcs Hex1ToBin1b ; Error, is smaller than A
	cpi rmp,16 ; bigger than F?
	brcs Hex1ToBin1c ; No, digit ok
Hex1ToBin1b: ; Error
	sbiw ZL,1 ; one back
	set ; Set flag
Hex1ToBin1c:
	ret ; Return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -