baileys 0 Newbie Poster

Title tell me
.model small
.stack 100h
.data

msg1 db 0Dh,0Ah, "TYPE A DECIMAL NUMBER >> $"
msg2 db 0Dh,0Ah,0Dh,0Ah, " In BINARY >> $"
buffer db 7,?, 5 dup (0), 0, 0
binary dw ?
illegal db 10,13,'not valid, TRY AGAIN: ','$'
ctr DB ?
PROMPT_2 DB 0DH,0AH,0Dh,0Ah,' In HEXADECIMAL >> $'

.code
mov ax,3
int 10h

mov ax,@data
mov ds,ax
mov es,ax


;==========================================================
start:
mov dx, offset msg1
mov ah, 9
int 21h

; input string:
mov dx, offset buffer
mov ah, 0ah
int 21h

; make sure the string is zero terminated:
mov bx, 0
mov bl, buffer[1]
mov buffer[bx+2], 0


lea si, buffer + 2 ; buffer starts from third byte.
call tobin

; the number is in cx register.
; for '-1234' it's 0fb2eh

mov binary, cx

jcxz stop

; print pre-result message:
mov dx, offset msg2
mov ah, 9
int 21h

; print result in binary:
mov bx, binary
mov cx, 16
print: mov ah, 2 ; print function.
mov dl, '0'
test bx, 1000000000000000b ; test first bit.
jz zero
mov dl, '1'
zero: int 21h
shl bx, 1
loop print
; print binary suffix:
;mov dl, 'b'
;int 21h

;;something is wrong in this part
mov ah, 0ah
int 21h

; XOR BX, BX ; clear BX
; MOV DH, ah ; initialize DH with 0

CMP AL, 0DH ; comapre Al with CR
; JNE @SKIP ; jump to label @SKIP if AL!=CR

; CMP DH, 1 ; compare DH with 1
; JMP @END ; jump to label @END

@SKIP: ; jump label

INC DH ; increment DH by 1
AND AL, 0FH ; convert the ascii into decimal code

SHL BX, 1 ; shift BX towards left by 1 position
OR BL, AL ; set the LSB of BL by AL

CMP DH, 16 ; compare DH with 16
JE @END ; jump to label @END if DH=16


@END: ; jump label

LEA DX, PROMPT_2 ; load and display the string PROMPT_2
MOV AH, 9
INT 21H

MOV CX, 4 ; initialize loop counter
MOV AH, 2 ; set output function

@LOOP_1: ; loop label
XOR DX, DX ; clear DX

@LOOP_2: ; jump label
SHL BX, 1 ; shift BX towards left by 1 position
RCL DL, 1 ; rotate DL towards left by 1 position
INC DH ; increment DH
CMP DH, 4 ; compare DH with 4
JNE @LOOP_2 ; jump to label @LOOP_2 if DH!=4

CMP DL, 9 ; compare DL with 9
JBE @NUMERIC_DIGIT ; jump to label @NUMERIC_DIGIT if DL<=9
SUB DL, 9 ; convert it to number i.e. 1,2,3,4,5,6
OR DL, 40H ; convert number to letter i.e. A,B...F
JMP @DISPLAY ; jump to label @DISPLAY

@NUMERIC_DIGIT: ; jump label
OR DL, 30H ; convert decimal to ascii code

@DISPLAY: ; jump label
INT 21H ; print the character
LOOP @LOOP_1 ; jump to label @LOOP_1 if CX!=0

jmp exit2

jmp start ; loop

stop:

ret ; return control to the operating system.

; this procedure converts string number to
; binary number. number can have a sign ('-').
; the result is stored in cx register.
; parameters:
; si - address of string number (zero terminated).

tobin proc near
push dx
push ax
push si


jmp process

;==== local variables ====
make_minus db ? ; used as a flag.
ten dw 10 ; used as multiplier.
;=========================

process:

; reset the accumulator:
mov cx, 0

; reset flag:
mov cs:make_minus, 0

next_digit:

; read char to al and
; point to next byte:
mov al, [si]
inc si

; check for end of string:
cmp al, 0 ; end of string?
jne not_end
jmp stop_input
not_end:

; check for minus:
cmp al, '-'
jne ok_digit
mov cs:make_minus, 1 ; set flag!
jmp next_digit

ok_digit:

; multiply cx by 10 (first time the result is zero)
push ax
mov ax, cx
mul cs:ten ; dx:ax = ax*10
mov cx, ax
pop ax

; it is assumed that dx is zero - overflow not checked!

; convert from ascii code:
sub al, 30h

; add al to cx:
mov ah, 0
mov dx, cx ; backup, in case the result will be too big.
add cx, ax

; add - overflow not checked!

jmp next_digit

stop_input:

; check flag, if string number had '-'
; make sure the result is negative:
cmp cs:make_minus, 0
je not_minus
neg cx

not_minus:

pop si
pop ax
pop dx
ret

exit2: mov ah,7
int 21h

mov ah,4ch
int 21h


tobin endp
end