All pastes #25880 Raw Edit

Anonymous

public text v1 · immutable
#25880 ·published 2005-10-18 20:20 UTC
rendered paste body
comment #
Author:		Vincent Castellano
Date:		10/18/05
Class:		CSIS 165
Assignment:	2. ASCII to Binary
		Write an AtoB fucntion, which is essentially an asm implimentation
		of the function atoi. Converts an ascii string of numbers to it's
		binary representation.
#

.586
.model flat, stdcall
.stack 1024

.data
binary	dword	?			;double word answer
ascii	byte	"123456789",0		;input null-terminated string

.code
main PROC
		call AtoB		;convert ASCII to Binary
		nop			;landing spot for debugger
main ENDP
	
AtoB PROC
	mov	eax, 0			;clear out eax
	mov 	ebx, offset ascii	;load address of first char into ebx
	mov 	esi, 10			;store multiple per digit
calc:	movsx	ecx, byte ptr [ebx]	;store first digit char
	sub 	ecx, 30h		;subtract offset on ascii
	inc	ebx			;move ebx addr to next char
	add	eax, ecx		;add digit value into accum
	cmp	byte ptr [ebx],0	;see if we just grabbed the last digit
	je	nmult			;last digit, don't multiply for ones place value
	mul	esi			;multiply value by 10 to get new place value
nmult:	cmp	byte ptr [ebx], 0	;compare character value to our EOL
	ja	calc			;jump if the char value is greater then 0 (if not, EOL)
	mov	binary, eax		;store answer
	ret		
AtoB ENDP

END main	 			;lead in

comment #
For values of:

"429496729"
x00403FF3  00 00 00 00 00 00 00 00 00 00 00 00 00 99  .............
0x00404001  99 99 19 34 32 39 34 39 36 37 32 39 00 00  .429496729..

"0"
0x00403FF3  00 00 00 00 00 00 00 00 00 00 00 00 00 00  ..............
0x00404001  00 00 00 30 00 00 00 00 00 00 00 00 00 00  ...0..........

"123456789"
0x00403FF3  00 00 00 00 00 00 00 00 00 00 00 00 00 15  ..............
0x00404001  cd 5b 07 31 32 33 34 35 36 37 38 39 00 00  [.123456789..
#