I want to put a box around lines of code and to add comments inside the box.
This is what I want it to achieve:

This is the example of code I am using for the algorithm.
\documentclass{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{subcaption}% http://ctan.org/pkg/subcaption
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}
\begin{document}
\begin{table}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}%\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$%\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo1}
\end{subalgorithm}%
\begin{subalgorithm}{.5\textwidth}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}%\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}%\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$%\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{algo2}
\end{subalgorithm}
\captionsetup{labelformat=alglabel}
\caption{Two algorithms}%
\label{tab:1}%
\end{table}
\end{document}
