5

So I found this picture online an similar ones in a book and wanted to recreate them for my term paper.

enter image description here

I'm not that skilled when it comes to TikZ and I'm always struggling with arrows and placement. -.- But I tried (at least):

\documentclass[tikz]{standalone}

\usepackage[T1]{fontenc}

\usepackage{tikz}
    \usetikzlibrary{calc}
    \usetikzlibrary{positioning}
    \usetikzlibrary{shapes.geometric}
    \usetikzlibrary{arrows}

\begin{document}
    \begin{tikzpicture}[
        >=stealth,
        node distance = 1.25cm,
        bit/.style = { rectangle, minimum width = .75cm, minimum height = .5cm, fill = gray!5, draw },
        arrow/.style = { -> }]

        \node[bit, label = below:$ 7 $](bit7){};
        \node[bit, right of = bit7, label = below:$ 6 $](bit6){};
        \node[bit, right of = bit6, label = below:$ 5 $](bit5){};
        \node[bit, right of = bit5, label = below:$ 4 $](bit4){};
        \node[bit, right of = bit4, label = below:$ 3 $](bit3){};
        \node[bit, right of = bit3, label = below:$ 2 $](bit2){};
        \node[bit, right of = bit2, label = below:$ 1 $](bit1){};
        \node[bit, right of = bit1, label = below:$ 0 $](bit0){};

        \draw [arrow] (bit7) -- (bit6);
        \draw [arrow] (bit6) -- (bit5);
        \draw [arrow] (bit5) -- (bit4);
        \draw [arrow] (bit4) -- (bit3);
        \draw [arrow] (bit3) -- (bit2);
        \draw [arrow] (bit2) -- (bit1);
        \draw [arrow] (bit1) -- (bit0);

    \end{tikzpicture}
\end{document}

As you will see, it ain't that pretty. -.- When I reduce the node distance, the arrows all look messed up and I can't find a way to draw a useful arrow from 0 to 7.

enter image description here

And as for the SWAP figure I got no plan at all. :( So a little help would be much appreciated. :)

6

I'm sure you'll find some other solutions here. This is one more. It uses a matrix of nodes to declare bit cells.

Separation between columns is declared with column sep option, but can be modified with [<increasing distance>] after & in desired column. This possibility has been used in RLCA register to introduce a certain distance between carry bit and register.

By default, matrix nodes are called after matrix name-row-column, but a new name can be assigned with syntax |(name)| as you can see in every second row of both matrices.

On first example (RCA), register label was added as a matrix label, but on RLCA, labels were drawn as regular nodes after matrix construction. They can not be included as labels in bit nodes because their size modifies distances between cells.

The SWAP register also uses a matrix but nodes on second row are not drawn. After that two fit nodes are used to define high and low nibbles. With fitting nodes, their contents has to be written with label=center:... as shown below.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows.meta,fit}

\begin{document}
\begin{tikzpicture}[>=Stealth,
font=\ttfamily,
bits/.style={draw, minimum size=7mm},
register/.style={matrix of nodes,nodes={bits},
column sep=-\pgflinewidth, 
row sep=0.5mm, nodes in empty cells,
row 1/.style={nodes={draw=none, minimum size=5mm}},
}]

\matrix[register, label=below:RLA] (RLA) {
7 & 6 & 5 & 4 & 3 & 2 & 1 & 0\\
|(7)|&|(6)|&|(5)|&|(4)|&|(3)|&|(2)|&|(1)|&|(0)|\\};

\foreach \i in {0,...,7}
    \draw[->] ([xshift=1mm]\i.east)--++(180:4mm);
\draw[->] (7.west)--++(180:3mm);
\draw (7.west)--++(180:1cm)|-(RLA.north)-|([xshift=1cm]0.east)--(0.east);

\begin{scope}[yshift=-3cm]
\matrix[register] (RLCA) {
C &[5mm] 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0\\
|(C)| & |(7)|&|(6)|&|(5)|&|(4)|&|(3)|&|(2)|&|(1)|&|(0)|\\};

\node[below] at (C|-RLCA.south) {Carry flag};  
\node[below] at (4.east|-RLCA.south) {RLCA};

\foreach \i in {0,...,7}
    \draw[->] ([xshift=1mm]\i.east)--++(180:4mm);
\draw[->] (7)--(C);
\draw[->] (C.west)--++(180:3mm);
\draw (C.west)--++(180:1cm)|-(RLCA.north)-|([xshift=1cm]0.east)--(0.east);
\end{scope}

\begin{scope}[yshift=-6cm]
\matrix[register, row 2/.style={nodes={bits,draw=none}}] (SWAP) {
7 & 6 & 5 & 4 & 3 & 2 & 1 & 0\\
|(7)|&|(6)|&|(5)|&|(4)|&|(3)|&|(2)|&|(1)|&|(0)|\\};

\node[fit={(7.north west) (4.south east)}, inner sep=0pt, draw, label=center:High Nibble] (HN) {};  
\node[fit={(4.north east) (0.south east)}, inner sep=0pt, draw, label=center:Low Nibble] (LN) {};  

\draw (HN.south)--++(-90:3mm)-| (LN.south) node[pos=0.25, below] (swap) {SWAP A};
\draw [<->] (swap.north)+(-6mm,0)--++(0:6mm);
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

  • Thank you @Ignasi, that's exactly what I was looking for. :) – Chris.V Jun 28 '15 at 8:45
  • @Chris.V Glad to help you. – Ignasi Jun 28 '15 at 8:49
0

Short partial solution :

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \draw[scale=.5] let \n1={7} in grid (\n1+1,1) +(.5,1) rectangle (-.5,.5)
      [xshift=.5cm] foreach \i in {0,...,\n1}{ 
        (\n1-\i,1.5) node{\i} (\i+1,.5) edge[-latex] (\i,.5)};
  \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.