zinsu_90
zinsu_90

Reputation: 31

Assembly home-work

The program is part of homework, the problem is - the program gets stuck (freeze).

By using Turbo Debugger I know that the program works, and returns the correct value (0). But, do not know why, the output is not displayed on the screen and the program gets stuck.

The program

 .MODEL SMALL
    .STACK 100h
    .DATA
ANSWER_OUTPUT DB 'The last digit is : x',13,10,'$'
ARR1 DB 1 DUP(123,2,65)
ARR2 DB 1 DUP(1,15,54)
ARR3 DW 3 DUP(0)
MIN_NUMBER DW 0
TEN DW 10
HELP DW 0
TWO DB 2
.CODE
.386
START: 
     MOV AX,@DATA                   ; DS can be written to only through a register
     MOV DS,AX                      ; Set DS to point to data segment
     MOV DI,0
     MOV SI,2
GET_ARR3:
     MOV AL,ARR1[SI]
     CMP AL,ARR2[DI]
     JAE BIGGER
     JMP SMALLER
BIGGER:
     MOV AX,0
     MOV AL,ARR2[DI]
     MOV HELP,AX
     MOV AX,0
     MOV AL,ARR1[SI]
     DIV HELP
     MOV CX,DX
     MOV AX,DI
     MUL TWO
     MOV DI,AX
     MOV ARR3[DI],CX
     DIV TWO
     MOV DI,AX
     INC DI
     SUB SI,1
     CMP DI,3
     JNE GET_ARR3
     JMP FIND_MIN
SMALLER:
     MOV AX,0
     MOV AL,ARR2[DI]
     MOV HELP,AX
     MOV AX,0
     MOV AL,ARR1[SI]
     MUL HELP
     MOV CX,AX
     MOV AX,DI
     MUL TWO
     MOV DI,AX
     MOV ARR3[DI],CX
     DIV TWO
     MOV DI,AX
     INC DI
     SUB SI,1
     CMP DI,3
     JNE GET_ARR3
     MOV AX,0
     MOV AX,ARR3[0]
     MOV MIN_NUMBER,AX
FIND_MIN:
     MOV AX,0
     MOV AX,ARR3[1]
     CMP AX,MIN_NUMBER
     JB CASE_1
     JMP CASE_11
CASE_1:
     MOV MIN_NUMBER,AX
CASE_11:
     MOV AX,0
     MOV AX,ARR3[2]
     CMP AX,MIN_NUMBER
     JB CASE_2
     JMP FIND_DIGIT
CASE_2:
     MOV MIN_NUMBER,AX           
FIND_DIGIT: 
     MOV AX,0
     MOV AX,MIN_NUMBER
     CMP AX,TEN
     JB End_Asm
     DIV TEN
     MOV AL,AH
     MOV AH,0
     MOV MIN_NUMBER,AX
     MOV AX,0
     JMP FIND_DIGIT
End_Asm:
     MOV AX,MIN_NUMBER
     ADD AL,'0'
     MOV ANSWER_OUTPUT[20],AL
     MOV DX,OFFSET ANSWER_OUTPUT    ; Set  DS:DX to point to question_output         
     MOV AH,9                       ; Set print option for int 21h
     INT 21h                        ; Print question_output
     MOV AH,4Ch                     ; Set terminate option for int 21h
     INT 21h                        ; Return to DOS (terminate program)
END START

Upvotes: 2

Views: 89

Answers (1)

Sep Roland
Sep Roland

Reputation: 39191

Because you defined HELP as a word (HELP DW 0), the instruction DIV HELP will divide DX:AX by HELP. Therefore you need to zero DX:

xor dx, dx
DIV HELP

Because you defined TEN as a word (TEN DW 10), the instruction DIV TEN will divide DX:AX by TEN. Therefore you need to zero DX:

xor dx, dx     <-- You need to change the code (remainder is in DX)
DIV TEN

However in this case you should just change the definition of TEN to be a byte and keep the existing code. That's what the existing code is expecting anyway!

TEN db 10       <-- You can keep existing code (remainder is in AH)

Because you defined the ARR3 array as word (ARR3 DW 3 DUP(0)) you can address its elements using offsets like 0, 2, and 4.
Your program mistakenly uses

  • MOV AX,ARR3[1] where mov ax, ARR3[2] would be correct
  • MOV AX,ARR3[2] where mov ax, ARR3[4] would be correct

Looking at the above mistakes I find it hard to believe that "It run perfectlly in the TurboDebugger".
You might have gotten that "correct value (0)" purely by chance.

Upvotes: 2

Related Questions