简体   繁体   中英

Executing the file loops infinitely but the assembler did not report any errors

.MODEL SMALL
.STACK 100H
.DATA
    
    ARRAY DB 1,9,8,3,4,7

.CODE
    MAIN PROC
        MOV AX,@DATA
        MOV DS,AX

        MOV SI,OFFSET ARRAY
        MOV CX,6
        MOV BL, [SI]

        LOOPX:
            CMP [SI], BL
            JGE UPDATE
            RESUME:
            INC SI
            LOOP LOOPX

            ADD BL,51
            MOV DL,BL
            MOV AH,2

            UPDATE:
            MOV BL,[SI]
            JMP RESUME
    MAIN ENDP
END MAIN

I want to read the largest number in the array.

The program is incomplete

  • With MOV DL,BL MOV AH,2 you have setup for using the DOS.PrintCharacter function 02h, but you forgot to actually invoke it. Just add int 21h .
  • You forgot to end the program! You have allowed the code to fall-through into UPDATE where a jump to RESUME will create an infinite loop. Use the DOS.Terminate function 4Ch.

The program has an error/typo

To convert the single digit number into a printable character you need to add 48 , so ADD BL,51 is wrong.

The program is not optimal

  • The first array element gets compared to itself and triggers a redundant reload of BL. In your array with 6 elements, this code needs 5 comparisons to arrive at the result.

  • Because you want to output the result through the DL register, it would be better to use DL from the start.

  • Unless you program for the 8086, it would be better to replace loop... by dec cx jnz...

     MOV SI, OFFSET ARRAY MOV CX, 6 - 1; 5 comparisons MOV DL, [SI]; First element LOOPX: INC SI CMP [SI], DL JLE DONTUPDATE MOV DL, [SI] DONTUPDATE: DEC CX JNZ LOOPX ADD DL, 48; You could write this as ADD DL, '0' MOV AH, 02h INT 21h MOV AX, 4C00h INT 21h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM