Search code examples
assemblyx86-16

reading lines in Assembly


I need to create a program in Assembly that can count characters in one line and save the length of the shortest into a variable. But I don't really understand what to do and can't find help anywhere.

I have this, but it’s far from what I want it to do.

cpu 8086
segment code
start   mov bx,data
mov ds,bx
mov bx,stack
mov ss,bx
mov sp,dno

radek   mov ah,0x0a
mov dx,nacteno
    int 21h
mov al,[nacteno+1]
cmp al,0
jne size
jz konec

size    mov bl,[rslt34]
cmp al,bl
jle radek
mov [rslt34],al
jmp radek
konec   hlt

segment data
nacteno db 255, ?
resb 255
rslt34  db 0

segment stack
resb 16
dno:    db ?

Solution

  • that can count characters in one line

    Because you're using the DOS.BufferedInput function 0Ah there's not much counting needed since you get it for free in the second byte of the structure. So be it.
    Surprisingly perhaps, your attempt is rather good. But because of the way you initialize rslt34 (rslt34 db 0), the cmp al,bl jle radek will not close in on the shortest string but on the longest string.

    You should setup rslt34 to 255 and use next code:

      cmp  al, 0          ; Exit on empty string
      je   konec
    size:
      cmp  al, [rslt34]
      jnb  radek          ; Ignore if not below what we already have
      mov  [rslt34], al   ; Update with a length that is smaller than before
      jmp  radek
    konec:
    

    The length byte that DOS gives you must be treated as an unsigned number. Therefore don't use signed conditional jumps like jle and friends. Use the unsigned conditional jumps like jb (JumpIfBelow), jnb (JumpIfNotBelow), and others.


    segment stack
    resb 16
    

    Realistically, the BIOS/DOS stack should be at least 256 bytes, preferably 512 bytes.


    konec   hlt
    

    The usual way to exit from a DOS program uses next instructions:

    mov  ax, 4C00h    ; DOS.TerminateProgram
    int  21h