All pastes #2113585 Raw Edit

Anonymous

public text v1 · immutable
#2113585 ·published 2012-02-09 01:17 UTC
rendered paste body
; Assuming the address of the number to be converted from dec2bin is stored in A1
; the converted value will be stored in D1

Dec2bin		move.l D2, -(A7)		; store values of registers to be used onto stack, so they arnt modified
		move.l A1, -(A7)


dec2binloop	move.b (A1)+, D2	;move one byte into d2
		cmp.l #0, d2		; check to see if that byte is null
		beq exit_dec2bin	; if the byte moved is 0, then we're at the end and exit the conversion
		muls.l #10, D1		;multiply final result by 10 to prepare for additional characters to be added.
		sub.l #$30, D2		; subtract 30 from byte moved
		add.l d2, d1		;add the resultant byte to the final result
		jmp dec2binloop		;loop again, untill a null is found;

exit_dec2bin 	move.l (A7)+ , A1	;restore registers to original value		
		move.l (A7)+ , D2
		rts

******************************************
; wordsized binary number is stored in d1
; the address of the converted word is pointed at by A2 and onwards

bin2dec		move.l D1, -(A7)		; store registers onto stack so they are not modified
		move.l D2, -(A7)
		move.l D3, -(A7)
		move.l #0, -(a7)		; the stack will be used in this subroutine so add a null to separate used space and data to be not modified
		move.l #10, D3		; this register value will be used in the remu command
		
bin2decloop	cmp.l #0, D1		; see if the binary number is 0
		beq reverse_chars	;if the binary number is 0, we are done and reverse the order
		remu d3, d2:d1 		; else, divide the binary number by 10 and store the remainder in D2
		add.l #$30, D2		; add 30 to the remainder
		move.l d2 -(a7)		; put the result onto the stack
		jmp bin2decloop		;continue looping with the remaining binary words untill we hit 0.

reverse_chars	cmp.l #0, (a7)		; see if the stack is at null, meaning we're at the end of our conversion;
		beq exit_bin2dec	;if the stack is null, we are done and may restore original registers
		move.l (A7)+, D2	; move the value of the binary number into d2 
		move.b d2, (A2)+	; now store the values that were in the stack in reverse order (in reverse order, we're popping the stack) into A2
		jmp reverse_chars

exit_bind2dec 	move.l (A7)+,D2	; pop the final null into the pointer A2 to signify the end of our conversion result
		move.b D2, (A2)+	
		move.l (A7)+,D3	; restore old values :)
		move.l (A7)+,D2
		move.l (A7)+,D1