Assembly 8086. Read from the standard input several numbers, in base 2. Print on the screen these numbers in base 10

风流意气都作罢 提交于 2021-02-17 07:19:08

问题


So, i have to convert the numbers from base two in base 10, and i have no idea how to do it.This is how my program looks for now, but I am not sure if what I got until now works properly. The conversion part is really giving me a hard time, but for sure I have lots of mistakes in my code, so any help would be greatly appreciated.

   assume cs:code, ds:data

data segment
msg db 'Give numbers:$'
numbers LABEL BYTE
    max_size DB 100
    numbers_len DB ?
    number DB 100 dup (?)
ten db 10
errm db 'Numbers not in binary.$'
data ends
code segment
start:

mov ax,data
mov ds,ax

mov ah,09h
mov dx,offset msg
int 21h

mov AH,0Ah
mov DX,offset numbers
int 21h

mov CL,numbers_len
mov CH,0
mov SI,0    
repeat:
    mov AL,number[SI]
    cmp AL,' '
    je conversion

    check:
    cmp AL,'0'
    jb erro
    cmp AL,'1'
    ja erro

    jmp conversion
    continue:
    inc SI
    dec cx
    jcxz print 
    jmp repeat


conversion:

    jmp continue

print:
    pop dx
    add dl,'0'
    mov ah,02h
    int 21h
    loop print  
erro:
    mov ah,09h
    mov dx,offset errm
    int 21h

mov ax,4c00h
int 21h

code ends
end start

回答1:


First of all, it is not possible to perform base-2 to base-10 conversion directly : you have to convert the base-2 representation into an actual value in a register, and then turn that into base-10. This is because those bases are not related to each other, e.g. there is no way to obtain 10 by applying an integer power to 2.

Here's the code for the conversion from a base-2 string to a number. No error checking is performed, every character that's not 1 is simply treated as 0.

; input : bx - the starting address of the string, cx - its length
; output : ax - contents of the string interpreted as base-2
from_bin:
  mov di, bx
  add di, cx
  sub di, 1
  xor ax, ax
  mov dx, 1

.loop:
  cmp di, bx
  jb .end

  cmp byte [di], '1'
  jne .loop_next

  add ax, dx

.loop_next:
  shl dx, 1
  sub di, 1
  jmp .loop

.end:
  ret

As you can see, the code builds up the return value in ax by adding dx to it if there is a 1 at the current position. dx corresponds to the value of 2^n in every iteration of the loop, where n is the current position in the string, starting from zero.

The code to convert a value to base-10 uses division to convert the "native" base-2 value stored in a register into consecutive base-10 digits of that value.

; input : ax - the number to convert
; di - the last writable address of the output buffer
; output : di - the address of the last written character
; destroys bx, cx, dx
to_dec:
  mov cx, 10

.loop:
  xor dx, dx
  div cx

  mov bx, dx
  or bl, '0'
  mov [di], bl
  test ax, ax
  jz .end

  sub di, 1
  jmp to_dec

.end:
  ret

The conversion is done "backwards" : it starts from the lowest position and works its way up as long as the result of the division is nonzero - this means that there is still more to convert. This is also why the function needs to obtain the last writable address of the buffer instead of its beginning.



来源:https://stackoverflow.com/questions/34384754/assembly-8086-read-from-the-standard-input-several-numbers-in-base-2-print-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!