Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data segment
- newLine db 10,13,'$' ; "ENTER"
- firstMsg db "Wellcome to the online base convertor",10,13,"Please enter your base to convertor from",10,13,"<'H = Hex, 'D' = Dec, 'O' = Oct, 'B' = Bin>:",10,13,'$'; The entery msg
- failMsg db "Invalid input",10,13,"Please try again",10,13,'$' ; id input isn't ok
- strNum db "Please enter a number",10,13,'$'
- strHex db 5,?,5 dup(?)
- strBin db 17,?,17 dup(?)
- strOct db 6,?,6 dup(?)
- strDec db 7,?,7 dup(?)
- choose db "it is 68h",10,13,'$'
- HexResult db 10 dup(?),'$'
- DecResult db 13 dup(?)
- OctResult db 13 dup (?),'$'
- DecBin db 16 dup(?),'$'
- strJunk db "liora",10,13,'$'
- whatWedo db ?
- BinResult db 16 dup(?),'$'
- HowManyBins db ?
- data ends
- sseg segment stack
- db 100h dup (?)
- sseg ends
- code segment
- assume cs:code, ds:data, ss:sseg
- start: mov ax,data
- mov ds,ax
- firstInput: lea dx, firstMsg ; Print the first msg
- mov ah,9
- int 21h
- call CheckInput
- mov whatWedo,al
- cmp al, 68h
- je callHextoBin
- cmp al,48h
- je callHextoBin
- cmp al, 'D' ; Check if the input is 'D'
- je callDecToBIn
- cmp al, 'd' ; Check if the input is 'd'
- je callDecToBIn
- cmp al,'o'
- je callOctToBin
- cmp al,'O'
- je callOctToBin
- cmp al,'b'
- je callBinToAll
- cmp al,'B'
- je callBinToAll
- jmp exit
- callHextoBin: call HexToBin
- jmp exit
- callOctToBin: call OctToBin
- jmp exit
- callBinToAll: call BinToAll
- jmp exit
- callDecToBIn: call DecToBin
- exit: mov ah,4ch
- int 21h
- proc BinToAll
- xor bx,bx
- xor ax,ax
- mov al,16
- mov bl,strBin[1] ; How many in strBin
- sub al,bl ; How many '0' we need to add
- mov HowManyBins, bl
- mov cl,al ; Put it in cl for loop
- mov di,2
- mov si,0
- cmp cl,16
- jb FillEmpty
- FillEmpty: mov BinResult[si],'0'
- inc si
- loop FillEmpty
- mov cl, HowManyBins ; We need ? time to complete in cl for loop
- remain: mov dl,strBin[di]
- mov BinResult[si],dl
- inc di
- inc si
- loop remain
- call BinToDec
- lea dx, newLine ; Print new line
- mov ah,9
- int 21h
- call BinToHex
- lea dx, newLine ; Print new line
- mov ah,9
- int 21h
- call BinToOct
- ret
- endp BinToAll
- proc OctToBin
- mov cl,strOct[1] ; How many bytes to loop
- mov bx, 10 ; The multiplier
- xor dx,dx
- xor ax,ax
- xor dx,dx
- mov dx,10
- mov si,2
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- putOcts: add al,strOct[si]
- sub al,'0'
- cmp cl,1 ; If it is the last cell so don't mul
- je noMulOcts
- mul dx
- mov dx,10
- noMulOcts: inc si
- loop putOcts
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- mov bx,2
- mov si, 15
- axToBinOcts: cmp ax, 0
- je exitOtB ; If ax == 0 so this exit
- xor dx,dx ; We have to clear dx because the divide is between dx:ax to bx
- div bx
- cmp dl,1
- je putOneOtB
- mov BinResult[si],'0'
- dec si
- jmp axToBinOcts
- putOneOtB: mov BinResult[si],'1'
- dec si
- jmp axToBinOcts
- exitOtB:
- mov cx,si
- inc cx
- fillO: mov BinResult[si],'0' ; Fill the rest str with '0'
- dec si
- loop fillO
- lea dx,BinResult ; Print the binary msg
- mov ah,9
- int 21h
- lea dx,newLine ; Print the binary msg
- mov ah,9
- int 21h
- call BinToDec
- call BinToHex
- ret
- endp OctToBin
- proc DecToBin
- mov cl,strDec[1] ; How many bytes to loop
- mov bx, 10 ; The multiplier
- xor dx,dx
- xor ax,ax
- xor dx,dx
- mov dx,10
- mov si,2
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- putDecs: add al,strDec[si]
- sub al,'0'
- cmp cl,1 ; If it is the last cell so don't mul
- je noMul
- mul dx
- mov dx,10
- noMul: inc si
- loop putDecs
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- mov bx,2
- mov si, 15
- axToBin: cmp ax, 0
- je exitDtB ; If ax == 0 so this exit
- xor dx,dx ; We have to clear dx because the divide is between dx:ax to bx
- div bx
- cmp dl,1
- je putOneDtB
- mov BinResult[si],'0'
- dec si
- jmp axToBin
- putOneDtB: mov BinResult[si],'1'
- dec si
- jmp axToBin
- exitDtB:
- mov cx,si
- inc cx
- fill: mov BinResult[si],'0' ; Fill the rest str with '0'
- dec si
- loop fill
- lea dx,BinResult ; Print the binary msg
- mov ah,9
- int 21h
- lea dx,newLine ; Print the binary msg
- mov ah,9
- int 21h
- call BinToOct
- call BinToHex
- ret
- endp DecToBin
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- proc BinToHex
- mov si,16 ; Init the BINstr result index
- mov cl, 4 ; Init the external loop
- mov di,4 ; Because we went to put form the end to the start
- externalBtH:push cx ; Push for keep the the external loop index value
- xor ax,ax
- xor bx,bx
- mov dx,1 ; Init the accumolator counter
- mov cx, 4 ; Init the inner loop
- innerBtH: dec si
- mov bl,BinResult[si] ; Mov from the BinResult beginning to the end
- sub bl,'0' ; Convert the ascii to real decimal number
- ;shl bl,4 ; Shl 4 times to move the bits to the upper level
- loopBtH: shr bl,1 ; Shr 1 time to check the carry
- jc addToBtH ; If carry is 1 so add to the sum the DL VALUE
- shl dx,1 ; If not so shl 1 time
- loop innerBtH
- pop cx ; When finish thr inner loop so take the last external loop index value
- mov HexResult[di],al ; Put the the summerize value at the Result str
- add HexResult[di],'0' ; Add '0' to convert from number to the right char
- dec di
- loop externalBtH
- jmp exitBtH
- addToBtH: add ax,dx ; Add the Dl to summerize the bits value
- shl dx,1 ; Multiple by 2
- loop innerBtH
- pop cx
- cmp al,10
- jge putDigit
- mov HexResult[di],al
- add HexResult[di],'0'
- dec di
- loop externalBtH
- jmp exitBtH
- putDigit: mov HexResult[di],al
- add HexResult[di],37h
- dec di
- loop externalBtH
- exitBtH:
- mov HexResult[5],'<'
- mov HexResult[6],'H'
- mov HexResult[7],'E'
- mov HexResult[8],'X'
- mov HexResult[9],'>'
- mov HexResult[10],'$'
- lea dx, HexResult ; Print the binary msg
- mov ah,9
- int 21h
- ret
- endp BinToHex
- proc HexToBin
- lea dx, strHex[2]; Print the first msg
- mov ah,9
- int 21h
- xor cx,cx
- mov cl,strHex[1]
- mov bl,1
- mov si,0
- againHTB: inc bl
- push cx
- mov cx,4
- mov al, strHex[bx]
- startHTB: cmp al, 'A'
- jge digit
- sub al, 30h
- jmp shlToStart
- digit: sub al,37h
- shlToStart: shl al,4
- againSL:
- shl al,1
- jc putOne
- mov BinResult[si],'0'
- inc si
- loop againSL
- pop cx
- clc
- loop againHTB
- jmp exitHexToBin
- putOne: mov BinResult[si],'1'
- inc si
- loop againSL
- pop cx
- clc
- loop againHTB
- exitHexToBin: mov BinResult[si],10
- mov BinResult[si+1],13
- mov BinResult[si+2],'$'
- xor si,si
- lea dx, BinResult ; Print the binary msg
- mov ah,9
- int 21h
- cmp whatWedo,'h'
- je itIsHex
- cmp whatWedo,'H'
- je itIsHex
- jmp e
- itIsHex :
- call BinToDec
- call BinToOct
- e: ret
- endp HexToBin
- proc BinToOct
- mov cx, 5
- mov si,15
- mov di,4
- againBtO: xor ax,ax
- xor bx,bx
- mov bl,1
- push cx
- mov cx,3
- loopBtO: cmp BinResult[si], '1'
- je addOct
- shl bx,1
- dec si
- loop loopBtO
- mov OctResult[di], al
- add OctResult[di],'0'
- dec di
- jmp finishInnerOct
- addOct:add al,bl
- shl bx,1
- dec si
- loop loopBtO
- mov OctResult[di], al
- add OctResult[di],'0'
- dec di
- finishInnerOct: pop cx
- loop againBtO
- exitBinToOct: mov OctResult[5],'<'
- mov OctResult[6],'O'
- mov OctResult[7],'C'
- mov OctResult[8],'T'
- mov OctResult[9],'>'
- mov OctResult[10],10
- mov OctResult[11],13
- mov OctResult[12],'$'
- lea dx, OctResult ; Print the first msg
- mov ah,9
- int 21h
- ret
- endp BinToOct
- proc BinToDec
- mov cx,16
- mov si, 15
- xor bx,bx
- xor ax,ax
- mov bl, 1
- againBtD:
- cmp BinResult[si],'1'
- je addBtD
- shl bx,1
- dec si
- loop againBtD
- jmp startMakeDec
- addBtD: add ax,bx
- shl bx,1
- dec si
- loop againBtD
- startMakeDec:
- xor bx,bx
- xor dx,dx
- xor si,si
- mov si,4
- mov bx,10
- mov cx,100
- makeDec: div bx
- mov DecResult[si],dl
- add DecResult[si],'0'
- Dec si
- xor dx,dx
- cmp al,0
- je putZero
- loop makeDec
- putZero: cmp si, 0
- je exitBinToDec
- mov DecResult[si],'0'
- Dec si
- loop putZero
- exitBinToDec: mov DecResult[5],'<'
- mov DecResult[6],'D'
- mov DecResult[7],'E'
- mov DecResult[8],'C'
- mov DecResult[9],'>'
- mov DecResult[10],10
- mov DecResult[11],13
- mov DecResult[12],'$'
- lea dx, DecResult
- mov ah,9
- int 21h
- ret
- endp BinToDec
- proc CheckInput
- again: mov ah, 1
- int 21h ;ENTER a char
- lea dx, newLine ; Newline
- mov ah,9
- int 21h
- cmp al, 'h'
- je jmpH
- cmp al, 'H'
- je jmpH
- cmp al, 'd'
- je jmpD
- cmp al, 'D'
- je jmpD
- cmp al, 'o'
- je jmpO
- cmp al, 'O'
- je jmpO
- cmp al, 'b'
- je jmpB
- cmp al, 'B'
- je jmpB
- mov dx, offset failMsg ; invalid input
- mov ah,9
- int 21h
- jmp again
- okH: mov dx, offset strNum ; "Please enter a number"
- mov ah,9
- int 21h
- mov dx, offset strHex ; Input a number
- mov ah, 0Ah
- int 21h
- mov cl, strHex[1]
- mov bx,2
- JMP againH
- jmpB: jmp okB
- jmpH: jmp okH
- jmpO: jmp okO
- jmpD: jmp okD
- againH: cmp strHex[bx], '0'
- jb failH
- cmp strHex[bx], '9'
- jbe jmpNextH
- cmp strHex[bx],'A'
- JB failH
- cmp strHex[bx], 'F'
- jbe jmpNextH
- cmp strHex[bx],'a'
- jb failH
- cmp strHex[bx], 'f'
- jbe jmpNextH
- failH: mov dx, offset failMsg ; invalid input
- mov ah,9
- int 21h
- jmp okH
- okB: mov dx, offset strNum ; Please enter a number...
- mov ah,9
- int 21h
- mov dx, offset strBin ; Input a number
- mov ah, 0Ah
- int 21h
- ; mov dl,strBin[1]
- mov cl, strBin[1]
- mov bx,2
- againB: cmp strBin[bx],'0'
- je jmpNextB
- cmp strBin[bx],'1'
- je jmpNextB
- failB: mov dx, offset failMsg ; invalid input
- mov ah,9
- int 21h
- jmp okB
- jmpNextH: jmp nextH
- jmpNextB: jmp nextB
- jmpAgainH: jmp againH
- jmpAgainB: jmp againB
- jmpAgaino: jmp againO
- okO: mov dx, offset strNum ; Please enter a number...
- mov ah,9
- int 21h
- mov dx, offset strOct ; Input a number
- mov ah, 0Ah
- int 21h
- mov cl, strOct[1]
- mov bx, 2
- againO: cmp strOct[bx], '0'
- jb failO
- cmp strOct[bx],'9'
- jbe nextO
- failO: mov dx, offset failMsg ; invalid input
- mov ah,9
- int 21h
- jmp okO
- okD: mov dx, offset strNum ; Please input
- mov ah,9
- int 21h
- mov dx, offset strDec ; Input a number
- mov ah, 0Ah
- int 21h
- mov cl, strDec[1]
- mov bx, 2
- againD: cmp strDec[bx],'0'
- jb failD
- cmp strDec[bx], '9'
- jbe nextD
- failD: mov dx, offset failMsg ; invalid input
- mov ah,9
- int 21h
- jmp okD
- nextH: inc bx
- loop jmpAgainH
- mov strHex[bx],10
- mov strHex[bx+1],13
- mov strHex[bx+2],'$'
- jmp exitF
- nextB: inc bx
- loop jmpAgainB
- mov strBin[bx],'$'
- jmp exitF
- nextO: inc bx
- loop jmpAgaino
- mov strOct[bx],'$'
- jmp exitF
- nextD: inc bx
- loop againD
- mov strDec[bx],10
- mov strDec[bx+1],13
- mov strDec[bx+2],'$'
- jmp exitF
- exitF:
- ret
- endp CheckInput
- code ends
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement