-3

I got this assembly program from my mate..My teacher provided it .Unfortunately I missed it out..Please someone say me that what the program write for (input/output or objectives)

.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
    MOV DX,0
    MOV AH,1
    INT 21H

    WHILE_:
    CMP AL,0DH
    JE END_WHILE
    INC DX
    INT 21H
    JMP WHILE_

    END_WHILE:
    MAIN ENDP
END MAIN
  • From the segment model this was intended to produce a .COM executable. The old MS-DOS interrupt `21h` function `01h` reads the keyboard. So take a guess at the purpose of `CMP AL,0DH`. Aside: if the code was supplied by a teacher it is pretty strange, instead of looping back to the setup for the interrupt, it repeats the interrupt and branches to somewhere else. Also it doesn't do anything with `DX`. – Weather Vane May 11 '19 at 09:44
  • 1
    @WeatherVane - The fact that there's no call to terminate the program is odd as well. – David Wohlferd May 11 '19 at 09:45
  • So back to the question: the input is from the keyboard and the output might be a system error message. – Weather Vane May 11 '19 at 09:48
  • I am also confused about this.I am not understanding the entire code stands for – Shamim Mohammad May 11 '19 at 09:51
  • It also happened to me when I emulate this@WeatherVane – Shamim Mohammad May 11 '19 at 09:53
  • 1
    I think there is enough in the comments here to see that the program is incomplete. There is no output and it does not terminate properly. – Weather Vane May 11 '19 at 09:53
  • oh,yes :( ..Thank you so much for seeing this matter@WeatherVane – Shamim Mohammad May 11 '19 at 09:55

1 Answers1

2

I've commented it for you:

.MODEL SMALL     ; We're writing code for x86-16
.STACK 100H      ; Give us a stack of 256 bytes
.CODE            ; What follows is the code
MAIN PROC
    MOV DX,0     ; Set the counter keeping track of the length of the string to 0
    MOV AH,1     ; The DOS service we want is to read a character from standard input
    INT 21H      ; Call said DOS service

    WHILE_:
    CMP AL,0DH   ; Is the character we read a carriage return?
    JE END_WHILE ; if so jump to the end of the program
    INC DX       ; if not increment the counter keeping track of the string length
    INT 21H      ; Call the DOS service for reading the standard input again
    JMP WHILE_   ; Jump back to _WHILE

    END_WHILE:
    MAIN ENDP    ; Exit the procedure
END MAIN

For the record, you can write this Assembly much more concisely and in a more modern manner:

.model small
.stack 0x100        ; This style of hex literal is more common nowadays
.code
main proc
    mov ah, 1       ; We don't need to set this continually since nothing clobbers it
    mov dx, -1

    _while:
      inc dx
      int 0x21
      cmp al, `\r`  ; most modern assemblers will support string literals
      jne _while
    main endp
end main
0x777C
  • 993
  • 7
  • 21
  • There's a couple of errors in your answer. (a) Nowhere in these programs is any BIOS service invoked! `int 21h` is the DOS service interrupt. BIOS<>DOS. (b) The 2nd program loops 65535 times because you've placed the `inc dx` instruction right before `jne _while`! Only the CF is untouched by `inc`. The ZF is modified. – Sep Roland May 19 '19 at 19:12
  • Instead of a `dec dx` at the end, probably most efficient to just do `mov dx, -1` before the loop. (That's only 1 byte larger than xor-zeroing, same as the dec, so this is neutral for static code size and a win for instruction executed. Also, 16-bit `xor dx,dx` isn't actually dependency-breaking on modern CPUs because it leaves the top 2 bytes of EDX unmodified; those are accessible in 16-bit mode. Haswell and later don't rename 16-bit partial registers separately from the full register, unlike P6-family (PPro through Nehalem)). – Peter Cordes May 19 '19 at 21:51
  • 1
    So it's kind of a dirty little secret that xor-zeroing isn't particularly good in 16-bit code, except for code-size and/or to also set flags to a known state. (Which is why it was used in the first place originally, before out-of-order execution x86 CPUs or partial-register renaming made it worth it for CPU designers to detect and special-case it.) And 8-bit `mov reg,imm8` is always 2 bytes, so `mov cl,0` or whatever isn't worse than `xor cl,cl` in general; it varies by microarchitecture and surrounding code. Anyway, you could hoist `mov ah,1` out of the loop: This DOS call preserves AH. – Peter Cordes May 19 '19 at 21:55
  • 1
    @PeterCordes Just sorted it – 0x777C May 20 '19 at 10:30