Programming Help
     
Home Page

What's New Page

Downloads

Programming

Walkthroughs

Tips and Tricks

Solutions Desk

Guest Book Page

About

 

1. Greatest common divisor function (C/C++):

// start of code --------------

int gcd(int a, int b) {
int remainder=1;

while (remainder!=0) {
remainder = a % b;
a = b;
b = remainder;
}

return a;
}
// end of code --------------

2. Recursive greatest common divisor function (C/C++):

// start of code --------------

int gcd( int num1, int num2 )
{
 int remainder = num2 % num1;
 
 if ( remainder != 0 )
  return gcd( remainder,num1 );
 
 return num1;
}


// end of code --------------

3. Assembly code of conversion between number systems for x86 porcessors:

;;######### start of code #############

include "emu8086.inc"
.MODEL SMALL
.STACK 200H
.DATA
 
 CRLF DB 0DH,0AH,'$'
  
 WRONG DB 'Wrong entry !!!$'
 
 PROMPT DB "Enter ur choice: $"
 
 PROMPT2 DB 0ah,0dh,'Enter number: $'
 PROMPT3 DB 0ah,0dh,'Enter all the 8 bits(max) binary number: $'
 PROMPT4 DB 0ah,0dh,'Enter any hexadecimal number: $'
 ANSWER DB 0AH,0DH,'Answer: $'
 TEMP DW ?
 MENU1 DB '1. Decimal to binary.$'
 MENU2 DB 0ah,0dh,'2. Decimal to hexadecimal.$'
 MENU3 DB 0ah,0dh,'3. Binary to decimal.$'
 MENU4 DB 0ah,0dh,'4. Binary to Hexadecimal.$'
 MENU5 DB 0ah,0dh,'5. Hexadecimal to decimal.$'
 MENU6 DB 0ah,0dh,'6. Hexadecimal to binary.$'
 
 
      
 MENU0 DB 0AH,0DH,'0. EXIT$'
 
 TERMINATED DB 0AH,0DH,'Program terminated.$'
 s1 DB "00000000", 0
 sum DW 0  ; result.
.CODE

MAIN PROC

 CALL DISP_MENU

 CALL INDEC  ;takes menu number
 
 
@PROCID:   ;Switches to chosen menu

 CMP AL,030h
 JE EXIT
 
 CMP AL,031h  ; ||
 JE @DEC_TO_BIN
 
 CMP AL,032h
 JE @DEC_TO_HEX  ; ||
 
 
  
 CMP AL,033h
 JE @BIN_TO_DEC  ; ||
 
 CMP AL,034h 
 JE @BIN_TO_HEX  ; ||
 
  
 CMP AL,035H
 JE @HEX_TO_DEC  ; ||
 
 CMP AL,036H
 JE @HEX_TO_BIN  ; ||

;-------------------------

@DEC_TO_BIN:

 CALL DEC_TO_BIN  ; convert n exit
 JMP EXIT
  
@DEC_TO_HEX:
 
 CALL DEC_TO_HEX  ; convert n exit
 JMP EXIT 


@BIN_TO_DEC:
 
 CALL BIN_TO_DEC  ; convert n exit
 JMP EXIT

@BIN_TO_HEX:
 
 CALL BIN_TO_HEX  ; convert n exit
 JMP EXIT


@HEX_TO_DEC:
 CALL HEX_TO_DEC
 JMP EXIT

@HEX_TO_BIN:

 CALL HEX_TO_BIN  ; convert n exit
 JMP EXIT
 

;-------------------------

EXIT:
 
mov AH,9
LEA DX,TERMINATED
INT 21H
    ; EXIT from the program
MOV AH,4CH
INT 21H

MAIN ENDP

;----------------------
WRONG_INPUT PROC NEAR
 
 MOV AH,9
 LEA DX,CRLF 
 INT 21H   ; prints a new line with message
 LEA DX,WRONG
 INT 21H
 LEA DX,CRLF 
 INT 21H
 RET
 
WRONG_INPUT ENDP
;----------------------


;---------------------display-------------
DISP_MENU PROC

PUSH AX
PUSH DX    ; save registers

 MOV AX,@DATA
 MOV DS,AX
 
 LEA DX,PROMPT  ; prints prompt message

MOV AH,9
INT 21H
 
 LEA DX,CRLF
 INT 21H
 INT 21h
 
 LEA DX,MENU1  ; prints menu 1
 INT 21H
 LEA DX,MENU2  ; prints menu 2
 INT 21H
 LEA DX,MENU3
 INT 21H   ; prints menu 3
 LEA DX,MENU4
 INT 21H  
 LEA DX,MENU5  ; prints menu 4
 INT 21H
 LEA DX,MENU6
 INT 21H   ; prints menu 5
 
 
 LEA DX,CRLF
 INT 21H
 
 LEA DX,MENU0  ; prints menu 0
 INT 21H
 
 LEA DX,CRLF
 INT 21H
 INT 21H


POP DX    ; restore registers
POP AX
 RET
DISP_MENU ENDP
;--------------------------------------------------

;-----------------------------INDEC-----------------

INDEC PROC ;NEAR
;read a number
;input: none
;output: AX= number

 PUSH BX   ; saves registers
 PUSH DX

;print prompt

@BEGIN:
 MOV AH,9
 LEA DX,PROMPT2  ; prints prompt2's message
 INT 21H  
 
;total=0

 XOR BX,BX ; BX holds total

;negative = false

 ;XOR CX,CX ; CX holds sign
 
;read a charecter
 MOV AH,1
 INT 21H  ; character in AL
 
;case character of
 
 ;CMP AL,'-' ; minus sign?
 ;JE @MINUS ; yes, set sign
 ;CMP AL,'+' ; plus sign
 ;JE @PLUS ; yes, get another character
 ;JMP @REPEAT2 ; start processing characters

;@MINUS:
; MOV CX,1 ; negative= true
 
;@PLUS:
; INT 21H  ; read a character

;end case

@REPEAT2:
;if character is between '0' and '9'

 CMP AL,30H ; character >='0'
 JNGE @NOT_DIGIT ; illigal character
 CMP AL,39H ; character >'9'?
 JG @NOT_DIGIT ; no, illigal character
 
;then convert character to a digit
 
 AND AX,000FH ; convert to digit
 PUSH AX  ; save on stack

; total= total* 10 + digit

 MOV AX,10 ; get 10
 MUL BX  ; AX= total * 10
 POP BX  ; retrive digit
 ADD BX,AX ; total = total * 10 + digit
 
;read a character
 MOV AH,1
 INT 21H
 CMP AL,0DH ; carriage return
 JNE @REPEAT2 ; no, keep going
 
;untill CR

 MOV AX,BX ; store number in AX
 ADD AX,30H
 POP DX  ; restore registers
 POP BX
 RET
 
;here if illigal character entered

@NOT_DIGIT:
 
 CALL WRONG_INPUT
 JMP @BEGIN  ; go to beginning

INDEC ENDP
;-----------------------------------------------------

;-------------------Binary input----------------

BIN_INPUT PROC  ; input value will be in AX(hex)

@START0: 
 CALL print_crlf
 LEA DX,PROMPT2
 MOV AH,9
 INT 21H

 XOR CX,CX
 MOV BX,0  
 MOV AH,1
 INT 21H
WHILE_:
 
 CMP AL,0DH
 JE END_WHILE
 SHL BX,1  ;bx getting the value
 INC CX   ; cx counts total inputs
 AND AL,0FH
 
 OR BL,AL
 INT 21H
 JMP WHILE_

END_WHILE:
 CMP BL,30H  ;bl=0?
 JE EMPTY
 MOV AL,BL
 CBW   ; converts byte to word
 RET

EMPTY:
 CALL print_crlf
 LEA DX,WRONG
 MOV AH,9  ; wrong message
 INT 21H
 JMP @START0

BIN_INPUT ENDP
;--------------------------------

;----------bin to dec conversion------
;input is in AX

CONVERT_BIN_TO_DEC PROC


MOV BL,1
MOV CX,8
MOV DX,AX  ;output from dx

LOOPTOP:
 SHL DL,1 
 
 JNC N  ;if cf=0
 MOV AL,1
 JMP M
N: MOV AL,0
M:
 SUB AL,30H
 MUL BL
 
 ADD SUM,AX
 SHL BL,1
 
 LOOP LOOPTOP ; loops untill cx 0
 
 MOV AX,SUM
 XOR AX,0FH ; ax gets the final value


RET

CONVERT_BIN_TO_DEC ENDP

;-------------------------------

;-----Binary to decimal output---------------
   
BIN_TO_DECIMAL_OUTPUT PROC ;outputs the content of AX

  

;input: AX
;output: none

 PUSH AX
 CALL print_crlf
 LEA DX,ANSWER
 MOV AH,9
 INT 21H
 
 POP AX
 AND AX,0FFFFH
 ;SUB AX,30H

; get decimal digits
 XOR CX,CX ; CX counts digits
 MOV BX,40h ; BX has divisor (change here)
 
@REPEAT1:
 
 XOR DX,DX ; prepare high word for divident
 DIV BX  ; AX= quotient, DX= remainder
 PUSH DX  ; save remainder on stack
 INC CX  ; count= count+1
;untill
 OR AX,AX ; quotient=0
 JNE @REPEAT1 ; no, keep going
 
;convert digits to charecters and print
 
 MOV AH,2 ; print char function

; for count times DO

@PRINT_LOOP:
 POP DX  ; digit in DL
 OR DL,30H ; convert to charecter
 INT 21H  ; print digit
 LOOP @PRINT_LOOP ; loop untill done

;end for
 

RET

BIN_TO_DECIMAL_OUTPUT ENDP
;---------------------------------


;-------------Binary to decimal--------------
BIN_TO_DEC PROC
JMP Y ; at the end of the source code
X:

RET


BIN_TO_DEC ENDP
;---------------------------------------------


;------Binary to hex--------
BIN_TO_HEX proc

CALL BIN_INPUT

MOV BX,AX

CALL print_hex

RET
BIN_TO_HEX ENDP


;---------------------------

 

 

;----- decimal to binary-------------

DEC_TO_BIN PROC  ; output the content of BX in binary


PUSH AX
PUSH BX   ; save registers

;MOV DL,0DH,0AH,'Enter ur number: $'
;MOV AH,9
;INT 21H

CALL INDEC

sub ax,30h
MOV BX,AX  ; output fron bx
MOV CX,16

MOV AH,9

LEA DX,CRLF
INT 21H
INT 21H   ; prints 'answer'
LEA DX,ANSWER
INT 21H

MOV AH,2

LOOP_:
 
 ROL BX,1
 
 JNC @ZERO  ; CF!= 1?
 MOV DL,31H
 
 INT 21H
 LOOP LOOP_
 JMP @END_0
@ZERO: 
 MOV DL,30H  ; cf=0
 INT 21H 
 LOOP LOOP_
@END_0:
 MOV AH,9
 LEA DX,CRLF
 INT 21H
 
 POP BX
 POP AX   ; restore registers
 
 
 RET
 
DEC_TO_BIN ENDP
;-------------------------------

 

;--------HEXINPUT---------
;input is in Bx

HEXINPUT PROC


call print_crlf
lea dx,prompt2   ; prompt
mov ah,9
int 21h

 XOR BX,BX  ; clr bx
 MOV CL,4
@BEGIN_H: 
 MOV AH,1
 INT 21H
 
WHILE_H:
 CMP AL,0DH  ; is the input CR?
 JE END_WHILE_H
 
 CMP AL,39H  ; a digit?
 JG LETTER  ; no a letter
 
 
 CMP AL,0  
 JNGE @NOT_DIGIT_H ; al<0 ?
 CMP AL,9 
 JNLE @NOT_DIGIT_H ; al>9 ?
  
 AND AL,0FH
 JMP SHIFT

LETTER:
 CMP AL,'a'
 JNGE @NOT_LETTER_H ; al< 'a'?
 CMP AL,'f'
 JNLE @NOT_LETTER_H ; al> 'f' ?
 
 SUB AL,37H
SHIFT:
 SHL BX,CL
 OR BL,AL
 INT 21h
 JMP WHILE_H

END_WHILE_H:
 RET
 
@NOT_DIGIT_H:
 
 CALL print_crlf
 JMP @BEGIN_H   ; prints new line
 
@NOT_LETTER_H:

 call print_crlf
 JMP @BEGIN_H
 
HEXINPUT ENDP

;----------------------------

 

;=============hex to bin========
HEX_TO_BIN proc

;CALL HEXINPUT
call print_crlf
lea dx,PROMPT4
MOV AH,9
INT 21H

call hexi ; takes hex input
 

MOV BX,DX ;output will b from BX
MOV CX,16 ;16 bits

MOV AH,9

LEA DX,CRLF  ;print new line n 'answer'
INT 21H
INT 21H
LEA DX,ANSWER
INT 21H   ; prints 'answer: '

MOV AH,2

LOOP_X:
 
 ROL BX,1
 
 JNC @ZEROX  ; CF!= 1?
 MOV DL,31H
 
 INT 21H
 LOOP LOOP_X
 JMP @END_0X
@ZEROX:    ;CF=0
 MOV DL,30H
 INT 21H 
 LOOP LOOP_X
@END_0X:
   
call print_crlf 


ret
HEX_TO_BIN endp
;===============================

;----hexi---
hexi proc
   xor     dx,dx                 ;clear for input
aschexbin:


 MOV AH,1
        INT 21H
        CMP AL,0DH  ; input CR?
        JE notasc
       
       ; lodsb
        cmp     al,'0'                  ;< 0
        jb      notasc                  ;yes invalid character
        cmp     al,'9'                  ;<= 9
        jbe     astrip                  ;yes, strip high 4 bits
        and     al,05fh                 ;force upper case
        cmp     al,'A'                  ;< ascii A
        jb      notasc                  ;yes, invalid character
        cmp     al,'F'                  ;> ascii F
        ja      notasc                  ;yes, invalid character
        add     al,9                    ;ok, add 9 for strip
astrip:
        and     al,0fh                  ;strip high 4 bits
        mov     cx,4                    ;set shift count
        shl     dx,cl                  ;rotate EDX 4 bits
        xor     ah,ah                   ;zero out AH
        cbw
        add     dx,ax                 ;add digit to value
        
     
        
        
        jmp     aschexbin               ;continue

notasc:
 ret
 
hexi endp
;-----------

 

;=======hex to decimal=========
HEX_TO_DEC PROC
call hexi  ; input is in dx

RET
HEX_TO_DEC ENDP

;==============================


;;********************DEC INPUT HEX OUTPUT**********************

DEC_TO_HEX PROC
 
 
  call print_crlf
  call    read_num
  call    print_crlf
  call    print_hex
  call    print_crlf
  RET      

DEC_TO_HEX ENDP
; *********************************************
; print_crlf
; prints out a carriage return and linefeed
; *********************************************

print_crlf  proc

    mov   ah, 2h    ; print character function
    mov   dl, 0dh   ; carriage return
    int   21h       ; print it
    mov   dl, 0ah   ; linefeed
    int   21h       ; print it
   
  ret
print_crlf endp

; *********************************************
; read_num
; reads a number from keyboard input
; and stores it in BX in binary
; *********************************************

read_num  proc

;total = total * 10 + digit

    mov ah,9 
    lea dx,prompt2
    int 21h
    
   mov   bx, 0h      ; clear bx for number
 
  ;
  ; get digit from keyboard and
  ; convert it to binary
  ;
  newchar:
    mov   ah, 1h      ; keyboard input function
    int   21h         ; execute ^ with dos
    sub   al, 30h     ; ascii to binary
    jl    endinpt     ; jump if < 0
    cmp   al, 9d      ; is it > 9d?
    jg    endinpt     ; yes, not decimal digit
    cbw               ; byte in al to word in ax
   
  ;
  ; digit is now in AX
  ; multiply number in BX by 10d
  ;
    xchg  ax, bx      ; trade digit and number
    mov   cx, 10d     ; put 10d in CX
    mul   cx          ; number times 10d
    xchg  ax, bx      ; trade number and digit
 
  ;
  ; add digit in AX to number in BX
  ;
    add   bx, ax      ; add digit to number
    jmp   newchar     ; get next digit
 
  ;
  ; check to see what they entered
  ; if nothing, exit
  ; otherwise, return from process
  ;
  endinpt:
    cmp   bx, 0h      ; is it 0?
    jne @return      ; nope, return from process
    JMP EXIT            ; yup, exit program
 
@return:
 
  ret
read_num endp

; *********************************************
; print_hex
; prints in hex to the screen the contents of
; BX
; *********************************************

print_hex proc

    call print_crlf
    mov ah,9
    lea dx,answer
    int 21h

    mov   ch, 4h    ; number of digits to print
 
  ; start with first digit in BX
  ; register

  rotate:
    mov   cl, 4h    ; set count to 4 bits
    rol   bx, cl    ; left digit to right
 
  ; convert to number

    mov   al, bl    ; mov to AL
    and   al, 0fh   ; mask off left digit
    add   al, 30h   ; convert hex to ascii
    cmp   al, 3ah   ; is it > 9?
    jl    printit   ; jump if digit =0 to 9
    add   al, 27h   ; digit is A to F
 
  ; print the digit in
  ; the AL register

  printit:
    mov   dl, al    ; put ASCII char in DL
    mov   ah, 2h    ; display output function
    int   21h       ; call DOS
    dec   ch        ; done 4 digits?
    jnz   rotate    ; not yet
   
  ; fallthrough and return
 
  ret
print_hex endp
;;************************************************************************

;=======================
Y:
; Print the welcome message:
;PRINTN "Enter 8 char Binary (zeros/ones) number: "
call print_crlf
lea dx,prompt3
mov ah,9
int 21h
; Get string:
MOV DX, 9   ; buffer size (1+ for zero terminator).
LEA DI, s1
CALL GET_STRING

; Check that we really got 8 chars,
; this check is optional:
MOV CX, 8
MOV SI, 0
check_s:
 ; Terminated before
 ; reaching 9th char?
 CMP s1[SI], 0
 JNE ok1 
 JMP error_not_valid
ok1: 
 ; Wrong digit? Not 1/0?
 CMP s1[SI], 31h
 JNA ok2
 JMP error_not_valid  
ok2:
 INC SI
 LOOP check_s


; Start the conversion from
; string to value in SUM variable.

MOV BL, 1   ; multiplier.
MOV SI, 7   ; index.
MOV CX, 8   ; counter for a loop.

next_digit:
 
 MOV AL, s1[SI]  ; get digit.
 SUB AL, 30h
 MUL BL  ; no change to AX.
 ADD SUM, AX
 SHL BL, 1
 DEC SI          ; go to previous digit.
 LOOP next_digit

; Done. Converted number is in SUM.

;PRINTN " "
;PRINTN "The result is: "
CALL print_crlf
lea dx,ANSWER
mov ah,9
int 21h
;CALL print_crlf

; Print the result:
MOV  AX, SUM
CALL PRINT_NUM_UNS
JMP  stop_program

error_not_valid:
;PRINTN " "
;PRINTN "ERROR: NOT VALID INPUT!"
CALL print_crlf
lea dx,WRONG
mov ah,9
int 21h
CALL print_crlf

stop_program:

RET  ; EXIT FROM PROGRAM.

; Definition of procedures
; from Emu8086.inc:

DEFINE_GET_STRING
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM_UNS

JMP X
;========================

 END MAIN


 ;; ############ end of code #########