Skip to content

Instantly share code, notes, and snippets.

@CynthiaMaldonadoR
Created May 31, 2020 05:54
Show Gist options
  • Save CynthiaMaldonadoR/1edfcf85ddbc341c2589d41533456518 to your computer and use it in GitHub Desktop.
Save CynthiaMaldonadoR/1edfcf85ddbc341c2589d41533456518 to your computer and use it in GitHub Desktop.
name "Decimal a Binario"
org 100h
jmp start
;mensaje en pantalla:
msg1 db 0Dh,0Ah, "Ingrese cualquier numero de -32768 a 65535, o presione cero para salir: $"
msg2 db 0Dh,0Ah, "El numero binario es: $"
;buffer para int 21/0ah
;primer byte es en el tamano del buffer,
;segundo byte es el numero de chars actualmente leidos (establecer por int 21h/0ah).
buffer db 7,?,5dup (0), 0, 0
;para resultados
binary dw ?
start:
; imprime mensaje de bienvenida:
mov dx, offset msg1
mov ah, 9
int 21h
; input string:
mov dx, offset buffer
mov ah, 0ah
int 21h
;presionar 0 para terminar:
mov bx, 0
mov bl, buffer[1]
mov buffer[bx+2], 0
lea si, buffer + 2 ; buffer inicia el 3er byte.
call tobin
; el numero se guarda en el registro cx.
; de '-1234' it's 0fb2eh
mov binary, cx
jcxz stop
; imprime el pre resultado:
mov dx, offset msg2
mov ah, 9
int 21h
;imprime en forma binaria
mov bx, binary
mov cx, 16
print: mov ah, 2 ;imprime funcion
mov dl, '0'
test bx, 1000000000000000b ;teste el primer byte.
jz zero
mov dl, '1'
zero: int 21h
shl bx, 1
loop print
; imprime el suffix:
mov dl, 'b'
int 21h
jmp start ; loop
stop:
ret ; regresar el sistema del control.
; este procedimiento convierte un numero a string a
; numero binario. el numero puede tener el signo ('-').
; el resultado esta guardado en el registro cx.
; parametros:
; si - direccion de numero string (cero terminado).
tobin proc near
push dx
push ax
push si
jmp process
; === variables locales ===
make_minus db ? ; usando como una bandera.
ten dw 10 ; usando un multiplier.
;=========================
process:
; reiniciar el acumulador:
mov cx, 0
; resetear la bandera:
mov cs: make_minus, 0
next_digit:
; leer char a al y
; apuntar a el siguiente byte:
mov al,[si]
inc si
; checar el final del string:
cmp al, 0 ;termina el string?
jne not_end
jmp stop_input
not_end:
; chequeo para el minimo:
cmp al, '-'
jne ok_digit
mov cs:make_minus, 1 ;establecer bandera!
jmp next_digit
ok_digit:
; multiplicar cx por 10 (primera vez que el resultado es cero)
push ax
mov ax, cx
mul cs:ten ; dx:ax = ax*10
mov cx, ax
pop ax
; es asumido que dx is cero - overflox no cheacado
; convertir de codigo ascill :
sub al, 30h
; agregar al to cx:
mov ah, 0
mov dx, cx ; respaldo, en caso que el resultado sea muy grande.
add cx, ax
; agregar - overflow no checado
jmp next_digit
stop_input:
;checar bandera, si numero string tenia '-'
; estar seguro que el resultado es negativo:
cmp cs:make_minus, 0
je not_minus
neg cx
not_minus:
pop si
pop ax
pop dx
ret
tobin endp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment