3

Here is how I'm drawing it currently:

\documentclass{article}
\usepackage{tikz}
\usepackage[paperwidth=15cm]{geometry}

\begin{document}
\noindent

\begin{tikzpicture}
\node[draw,rectangle,text width=8cm,align=center] (qw) at (0,0) {\texttt{rax}} ;
\node[label={\footnotesize{63}}] (top) at (-4,0) {} ;
\node[label={\footnotesize{31}}] (low32) at (0,0) {} ;
\node[label={\footnotesize{15}}] (low16) at (2,0) {} ;
\node[label={\footnotesize{7}}] (low8) at (3,0) {} ;
\node[label={\footnotesize{0}}] (bot) at (4,0) {} ;
\node[draw,rectangle,text width=4cm,align=center,xshift=2cm,yshift=-0.7cm] (dw) {\texttt{eax}} ;
\node[draw,rectangle,text width=2cm,align=center,xshift=3cm,yshift=-1.4cm] (w) {\texttt{ax}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=2.5cm,yshift=-2.1cm] (hb) {\texttt{ah}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=3.5cm,yshift=-2.1cm] (lb) {\texttt{al}} ;
\end{tikzpicture}

\end{document}

It looks like this:

current image

Everything is fine except for the final row, where the two rectangles appear to be overlapping. There should only be one line in between ah and al—exactly in the middle. How do I fix this?

  • 1
    Maybe you want to use a dedicated package for such diagrams: ctan.org/pkg/bytefield – Henri Menke Jan 13 '19 at 22:38
  • While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz as there are numerous libraries. – Peter Grill Jan 13 '19 at 22:45
  • @PeterGrill sorry - fixed – Dan Jan 13 '19 at 23:12
3

You can set inner xsep=0pt:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage[paperwidth=15cm]{geometry}


\begin{document}
\noindent
\begin{tikzpicture}[inner xsep=0pt]
\node[draw,rectangle,text width=8cm,align=center] (qw) at (0,0) {\texttt{rax}} ;
\node[label={\footnotesize{63}}] (top) at (-4,0) {} ;
\node[label={\footnotesize{31}}] (low32) at (0,0) {} ;
\node[label={\footnotesize{15}}] (low16) at (2,0) {} ;
\node[label={\footnotesize{7}}] (low8) at (3,0) {} ;
\node[label={\footnotesize{0}}] (bot) at (4,0) {} ;
\node[draw,rectangle,text width=4cm,align=center,xshift=2cm,yshift=-0.7cm] (dw) {\texttt{eax}} ;
\node[draw,rectangle,text width=2cm,align=center,xshift=3cm,yshift=-1.4cm] (w) {\texttt{ax}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=2.5cm,yshift=-2.1cm] (hb) {\texttt{ah}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=3.5cm,yshift=-2.1cm] (lb) {\texttt{al}} ;
\end{tikzpicture}
\end {document}
3

Similar results but with bytefield package

\documentclass{article}
\usepackage{bytefield}
\usepackage{lmodern}
\begin{document}
\begin{bytefield}[endianness=big,
    bitwidth=.015\linewidth,
    boxformatting={\centering\sffamily}]{64}
\bitheader[endianness=big]{0,7,15,31,63}\\
\wordbox{1}{rax}\\[3mm]
\bitbox[]{32}{}
\bitbox{32}{eax}\\[3mm]
\bitbox[]{48}{}
\bitbox{16}{ax}\\[3mm]
\bitbox[]{48}{}
\bitbox{8}{al}\bitbox{8}{ah}
\end{bytefield}
\end{document}

enter image description here

  • Except for reversal of low and high bytes, looks good! Only seems much more complicated. +1 though for the demo! – Dan Jan 14 '19 at 12:29
  • Actually, after rereading it, perhaps it isn’t so complicated. I’d just need to learn the package – Dan Jan 14 '19 at 12:34
  • I ended up using bytefield in my final draft. – Dan Jan 15 '19 at 3:44
2

off-topic, for fun and exercises:

\documentclass{article}
\usepackage[paperwidth=15cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\noindent%
    \begin{tikzpicture}[
node distance = 3mm and 0mm,
   box/.style = {draw, text width=#1, align=center,minimum height=5mm,
                 inner sep=0pt, outer sep=0pt, font=\ttfamily},
   lbl/.style = {font=\footnotesize, inner sep=0pt, above=3mm}
                        ]
\node[box=8cm,right=of {(0,0)}] (qw) {rax};
\node[lbl] at (0,0) {63};
\node[lbl] at (4,0) {31};
\node[lbl] at (6,0) {15};
\node[lbl] at (7,0) {7} ;
\node[lbl] at (8,0) {0} ;
\node[box=4cm,below left=of qw.south east] (dw) {eax};
\node[box=2cm,below left=of dw.south east] (w)  {ax} ;
\node[box=1cm,below left=of w.south east]   (lb) {al} ;
\node[box=1cm,left=of hb]  (hb) {ah} ;
    \end{tikzpicture}
\end {document}

result:

enter image description here

addendum: one ore solution, using chains library:

\documentclass{article}
\usepackage[paperwidth=15cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{chains,positioning}

\begin{document}
\noindent%
    \begin{tikzpicture}[
node distance = 3mm and 0mm,
    start chain= going below,
   box/.style = {draw, text width=#1, align=center,minimum height=5mm,
                 inner sep=0pt, outer sep=0pt, font=\ttfamily,
                 on chain,anchor=north west},
   lbl/.style = {font=\footnotesize, inner sep=0pt, above=3mm}
                        ]
\node[box=8cm,right=of {(0,0)}] (qw) {rax};
\node[lbl] at (0,0) {63};
\node[lbl] at (4,0) {31};
\node[lbl] at (6,0) {15};
\node[lbl] at (7,0) {7} ;
\node[lbl] at (8,0) {0} ;
\node[box=4cm] (dw) {eax};
\node[box=2cm] (w)  {ax} ;
\node[box=1cm] (lb) {al} ;
\node[box=1cm,left=of lb] {ah} ;
    \end{tikzpicture}
\end {document}

the result is the same as before.

  • Except for low and high bytes being switched, looks good! +1 – Dan Jan 14 '19 at 12:27
  • 1
    @Dan, thank yo for pointing this. now is corrected. – Zarko Jan 15 '19 at 9:17

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.