minus equ $2D ; ASCII code of minus sign org $1000 in_buf fcc "-12889" ; input ASCII to be converted fcb 0 ; null character out_buf rmb 2 ; buf1 is used to hold the current digit value and buf2 will be cleared to 0 buf2 rmb 1 buf1 rmb 1 sign rmb 1 ; holds the sign of the number error rmb 1 ; indicates the occurrence of illegal character org $1500 clr sign clr error clr out_buf clr out_buf+1 clr buf2 ldx #in_buf ldaa 0,x cmpa #minus ; is the first character a minus sign bne continue ; branch if not minus inc sign ; set the sign to 1 inx ; move the pointer continue ldaa 1,x+ ; is the current character a NULL character? lbeq done ; yes, we reach the end of the string cmpa #$30 ; is the character not between 0 to 9? lblo in_error ; " cmpa #$39 ; " lbhi in_error ; " suba #$30 ; convert to the BCD digit value staa buf1 ; save the digit temporarily ldd out_buf ldy #10 emul ; perform 16-bit by 16-bit multiplication addd buf2 ; add the current digit value std out_buf bra continue in_error ldaa #1 staa error done ldaa sign ; check to see if the original number is negative beq positive ldaa out_buf ; if negative, compute its two’s complement ldab out_buf+1 ; “ coma ; “ comb ; “ addd #1 ; “ std out_buf positive swi end