Chris M
Chris M

Reputation: 33

GCD 8086 program

I am kind of new to 8086 programming, and it is a part of my subjects this semester. I find it a bit difficult but kinda still manage. We were asked to do a GCD program and here is what I have done, but I dont get the right answer, any input or suggestions to sharpen my knowledge would be really helpful.

;  Read a pair of 16bit numbers and compute their GCD.
.DATA
   N1 DW ?
   N2 DW ?
   A  DW ?

MSG1 DB 10,13, "ENTER 1ST 16 BIT HEXA NUMBER: $"
MSG2 DB 10,13, "ENTER 2ND 16 BIT HEXA NUMBER: $"
MSG3 DB 10,13, "GCD : $"



.CODE
START:
MOV AX, @DATA
MOV DS, AX 

MOV AH, 09H
LEA DX, MSG1
INT 21H  

LEA SI, N1
CALL READHEXA
MOV [SI+1], AL
CALL READHEXA
MOV [SI], AL
  

MOV AH, 09H
LEA DX, MSG2
INT 21H  

LEA SI, N2
CALL READHEXA
MOV [SI+1], AL
CALL READHEXA
MOV [SI], AL

MOV BX, N1
MOV AX, N2



   

; gcd start

   MAIN: CMP BX, AX
  JNE GCD

MOV AH, 09H
LEA DX, MSG3
INT 21H   

MOV AX, BX


LEA SI, AX
INC SI
CALL DISPHEXA
DEC SI
CALL DISPHEXA

MOV AH, 4CH
INT 21H

GCD: CMP BX,AX
 JC C1
 SUB BX,AX 
 
 JMP MAIN
C1:  SUB AX,BX
 DAS
 
 JMP MAIN
   



PROC READHEXA
PUSH CX ; GOOD PROGRAMMING ETIQUTTE

MOV AH, 01H ; INTERRUPT TO INPUT CHAR, INPUT IS IN AL
INT 21H             

SUB AL, 30H
CMP AL, 09H
JLE G1 ; IF HIGHER NIBBLE IS BETWEEN O TO 9 IT GOES TO G1
SUB AL, 07H

G1: MOV CL, 04H
    ROL AL, CL
    MOV CH, AL
    
MOV AH, 01H ; INTERRUPT TO INPUT CHAR, INPUT IS IN AL
INT 21H             

SUB AL, 30H
CMP AL, 09H
JLE G2 ; IF HIGHER NIBBLE IS BETWEEN O TO 9 IT GOES TO G1
SUB AL, 07H

G2: ADD AL,CH

POP CX

RET
   ENDP READHEXA  





DISPHEXA PROC NEAR
        
        MOV DL,[SI]   ;FIRST DIGIT
            
        MOV CL,04H
        SHR DL,CL
            
        CMP DL,09H
        JBE L1
        ADD DL,07H
                   
        L1: ADD DL,30H
            MOV AH,02H
            INT 21H
            
        
            
        MOV DL,[SI] ; SECOND DIGIT
        AND DL,0FH
            
        CMP DL,09H
        JBE L2
        ADD DL,07H
            
        L2: ADD DL,30H
            MOV AH,02H
            INT 21H 
            
            RET  ;RETURNS => MAIN PROGRAM
 ENDP  DISPHEXA 
  
    END START

Upvotes: 0

Views: 598

Answers (1)

vitsoft
vitsoft

Reputation: 5805

I appreciate that you decomposed the program to smaller parts - procedures. A GOOD PROGRAMMING ETIQUTTE however, is to complete each procedure with comments: what it does, what it expects on input, what is its output, which registers it changes. For instance:

PROC READHEXA ; reads two characters from keyboard. If they both are hexa digits 0..9 or A..F, it returns the corresponding value in AL.

PROC DISPHEXA ; displays the contents of a byte in memory addressed by DS:SI as two hexadecimal digits. Registers AX,CX,DX are clobbered.

PROC GCD ; (yet to be written) Calculates GCD of two WORD numbers in memory variables N1, N2 and stores the result in memory variable A.

It is then easier to debug. You should find out that when the calculation is finished at MAIN: CMP BX, AX and both registers are equal, you forgot to store the result from AX into memory variable A. Or change the contract of PROC GCD and let it return the result in AX.

Upvotes: 2

Related Questions