%--*- latex -*-----------------------------------------------------------------
%$Author: saulius $
%$Date: 2020-06-04 14:58:32 +0300 (Thu, 04 Jun 2020) $ 
%$Revision: 1524 $
%$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/seminarai/2020-verifikacjos-seminarui/slides.tex $
%------------------------------------------------------------------------------

\documentclass[mathserif]{beamer}
\usetheme{Warwick}
\useoutertheme{infolines}
\setbeamertemplate{headline}{} % removes the headline the infolines inserts
%\setbeamertemplate{footline}[frame number]
\renewcommand\familydefault{\rmdefault}
% For XeLaTeX:
% https://tex.stackexchange.com/questions/452151/how-do-i-render-the-word-v%C7%ABlundarkvi%C3%B0a-with-bookman-and-xelatex
% "Use an OpenType clone of Bookman, for instance TeX Gyre Bonum":
\usepackage{fontspec}
\setmainfont{TeX Gyre Bonum}

\usepackage[style=authoryear,maxnames=1,doi=true,url=true,backend=biber]{biblatex}
%\addbibresource{bibliography/citations.bib}
\addbibresource{bibliography/Gutierrez-Osuna.bib}
\addbibresource{bibliography/Intel.bib}
\newcommand{\mycite}{\parencite}


\usepackage{colordvi}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{snakes}
\usepackage{verbatim}
\usepackage{chemfig}
\usepackage{listings}

% https://en.wikibooks.org/wiki/LaTeX/Algorithms
% http://mirror.datacenter.by/pub/mirrors/CTAN/macros/latex/contrib/algorithmicx/algorithmicx.pdf
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{amssymb}

\include{commands}

\newcommand{\RCSid}[1]{\fontsize{7pt}{7pt}\selectfont $#1$ \today}


%%BEGIN LANGUAGE en
\title{Machine code. Assembler}
%%END LANGUAGE en


\author{Saulius Gražulis}

\date{Vilnius, 2020}

% Define colors as in
% https://venngage.com/blog/color-blind-friendly-palette/ ``Retro''
\definecolor{Bluish}{HTML}{63ACBE}
\definecolor{Magentish}{HTML}{601A4A}
\definecolor{Orangish}{HTML}{EE442F}

\begin{document}

\colorlet{IdentifierColor}{red!40!black}
\colorlet{StringColor}{green!70!black}
\colorlet{KwdColor}{Bluish}
\colorlet{CommentColor}{Orangish}

%------------------------------------------------------------------------------

\begin{frame}
    \titlepage

\input{affiliation}
    
    \begin{center}
      \mbox{}
      \hfill\hfill\hfill
      \includegraphics[height=1.5cm]{images/sp_VU_zenklas.eps}
      \hfill
      \includegraphics[height=1.5cm]{images/2019-05-02_Melynas_MIF-zenklas242x244.png}
      \hfill\hfill\hfill
      \mbox{}
    \end{center}

    \vfill

    %% \tiny
    %% \RCSid{
    %%   $Id: slides.tex 1524 2020-06-04 11:58:32Z saulius $
    %% }
    \begin{flushright}
      \begin{minipage}[c]{0.67\textwidth}
        \tiny\raggedright
        %%BEGIN LANGUAGE en
        This set of slides may be copied and used as specified in the
        %%END LANGUAGE en
        \myhref{http://creativecommons.org/licenses/by-sa/4.0/}{Attribution-ShareAlike
          4.0 International}
license
      \end{minipage}
      %%
      \begin{minipage}[c]{1.5cm}
        \myhref{http://creativecommons.org/licenses/by-sa/4.0/}{
          \includegraphics[width=1.5cm]{images/CC-BY-SA.eps}
        }
      \end{minipage}
    \end{flushright}

\end{frame}

%==============================================================================

\begin{frame}
\frametitle{Simple Harvard architecture processor}

  \begin{center}
    \includegraphics[width=12cm]{drawings/Harvard-CPU-Logisim/overal.png}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Instruction memory}

  \begin{center}
    \includegraphics[width=12cm]{drawings/Harvard-CPU-Logisim/data-tract.png}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\lstset{
  keywordstyle=\color{KwdColor},
  commentstyle=\color{CommentColor}\ttfamily,
  identifierstyle=\color{IdentifierColor},
  stringstyle=\color{StringColor},
  basicstyle=\ttfamily\tiny
}

\begin{frame}[fragile]
\frametitle{A simple algorithm}

\framesubtitle{Fibonacci numbers}

  \begin{center}
    \begin{minipage}{0.4\textwidth}
      \lstinputlisting[language=C,firstline=3,frame=trBL]{examples/Fibonacci-numbers/fibonacci.c}
    \end{minipage}
    \hspace{1cm}
    \begin{minipage}{0.4\textwidth}
      \scriptsize
    
      \begin{enumerate}
\item[\textit{Step 0}.] Set
        $a \leftarrow 1$, $b \leftarrow 1$.

($a$ and $b$ are two current sequence numbers)

\item[\textit{Step i}.] for
        $1 \le i \le N$:

        \begin{enumerate}[i]
          \scriptsize
\item temporarily save $b$:
          $t \leftarrow b\,;$

\item set
          $b \leftarrow a + b\,;$

\item output
          $b\,;$

\item set
          $a \leftarrow t\,;$
        \end{enumerate}

\item [\textit{Step $N+1$}.] Terminate the program
      \end{enumerate}
    \end{minipage}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Machine code}

  \begin{center}
    \begin{minipage}{0.4\textwidth}
      \lstinputlisting[language=C,firstline=3,frame=trBL]{examples/Fibonacci-numbers/fibonacci.c}
    \end{minipage}
    \hspace{1cm}
    \begin{minipage}{0.4\textwidth}
      \lstinputlisting[language=C,frame=trBL]{examples/Fibonacci-numbers/fib.hex}
    \end{minipage}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Assembler example}

  \begin{center}
    \begin{minipage}{0.4\textwidth}
      \lstinputlisting[language=C,firstline=3,frame=trBL]{examples/Fibonacci-numbers/fibonacci.c}
    \end{minipage}
    \hspace{1cm}
    \begin{minipage}{0.4\textwidth}
      \lstset{language=[Motorola68k]Assembler}
      \lstinputlisting[frame=trBL]{examples/Fibonacci-numbers/fib.asm}
    \end{minipage}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Instruction mnemonics}

  \begin{center}
    \begin{tabular}{ll|ll|l}
      \bf
Address
      &
      \bf
Machine code
      &
      \multicolumn{2}{l|}{\bf
Mnemonics
      } &
      \bf
Meaning
      \\
      \hline

      0000 &
      \color{KwdColor}02\color{IdentifierColor}0001 &
      \color{KwdColor}LDC & \color{IdentifierColor}1 &
      \tiny
Load const. 1 into the accumulator
      \\

      0001 &
      \color{KwdColor}04\color{IdentifierColor}0000 &
      \color{KwdColor}ST & \color{IdentifierColor}0 &
      \tiny
Store acc. to mem. location 0000
      \\

      0002 &
      \color{KwdColor}04\color{IdentifierColor}0001 &
      \color{KwdColor}ST & \color{IdentifierColor}1\\

      0003 &
      \color{KwdColor}02\color{IdentifierColor}0000 &
      \color{KwdColor}LDC & \color{IdentifierColor}0\\

      0004 &
      \color{KwdColor}04\color{IdentifierColor}0002 &
      \color{KwdColor}ST & \color{IdentifierColor}2\\

      0005 &
      \color{KwdColor}03\color{IdentifierColor}0001 &
      \color{KwdColor}LD & \color{IdentifierColor}1 &
      \tiny
      %%BEGIN LANGUAGE en
      Load accumulator with [0001]\footnote{\fontsize{6.6}{6.6}\selectfont
        [0001] -- contents of the data memory cell at the address 0001.}
      %%END LANGUAGE en
      \\

      0006 &
      \color{KwdColor}0B\color{IdentifierColor}0000 &
      \color{KwdColor}ADD & \color{IdentifierColor}0 &
      \tiny
Add contents of location 0000 to acc.
      \\

      ... \\

      
      000D &
      \color{KwdColor}1A\color{IdentifierColor}0015 &
      \color{KwdColor}SUBC & \color{IdentifierColor}15H &
      \tiny
Subtract 21 from accumulator
      \\

      000E &
      \color{KwdColor}0A\color{IdentifierColor}0005 &
      \color{KwdColor}JNEG & \color{IdentifierColor}0005 &
      \tiny
Jump to prog.\ address 0005 if acc. < 0
      \\

      000F &
      \color{KwdColor}FF\color{IdentifierColor}FFFF &
      \color{KwdColor}HALT & &
      \tiny
Halt the CPU
      \\

    \end{tabular}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Instruction operands}

  \begin{center}
    \begin{tabular}{lll}
      \textcolor{KwdColor}{N} & \color{KwdColor}EQU & 21 \\      
      \textcolor{KwdColor}{A} & \color{KwdColor}\only<1>{EQU}\only<2>{DS} & \only<1>{0}\only<2>{1} \\
      \textcolor{KwdColor}{B} & \color{KwdColor}\only<1>{EQU}\only<2>{DS} & 1 \\
      \textcolor{KwdColor}{I} & \color{KwdColor}\only<1>{EQU}\only<2>{DS} & \only<1>{2}\only<2>{1} \\
    \end{tabular}

    \vspace{0.5\baselineskip}
    \begin{tabular}{ll|ll|l}
      \bf
Address
      &
      \bf
Machine code
      &
      \multicolumn{2}{l|}{\bf
Mnemonics
      } &
      \bf
Meaning
      \\
      \hline

      0000 &
      \color{KwdColor}02\color{IdentifierColor}0001 &
      \color{KwdColor}LDC & \color{IdentifierColor}1 &
      \tiny
Load const. 1 into the accumulator
      \\

      0001 &
      \color{KwdColor}04\color{IdentifierColor}0000 &
      \color{KwdColor}ST & \color{IdentifierColor}A &
      \tiny
Store acc. to mem. location A
      \\

      0002 &
      \color{KwdColor}04\color{IdentifierColor}0001 &
      \color{KwdColor}ST & \color{IdentifierColor}B\\

      0003 &
      \color{KwdColor}02\color{IdentifierColor}0000 &
      \color{KwdColor}LDC & \color{IdentifierColor}0\\

      0004 &
      \color{KwdColor}04\color{IdentifierColor}0002 &
      \color{KwdColor}ST & \color{IdentifierColor}I\\

      0005 &
      \color{KwdColor}03\color{IdentifierColor}0001 &
      \color{KwdColor}LD & \color{IdentifierColor}B &
      \tiny
      %%BEGIN LANGUAGE en
      Load accumulator with [B]\footnote{\fontsize{6.6}{6.6}\selectfont
        [B] -- contents of the data memory cell that holds value of B.}
      %%END LANGUAGE en
      %% Load acc. from mem. location B
      \\

      0006 &
      \color{KwdColor}0B\color{IdentifierColor}0000 &
      \color{KwdColor}ADD & \color{IdentifierColor}A &
      \tiny
Add contents of location A to acc.
      %% Add contents of location A to acc.
      \\

      ... \\

      
      000D &
      \color{KwdColor}1A\color{IdentifierColor}0015 &
      \color{KwdColor}SUBC & \color{IdentifierColor}N &
      \tiny
Subtract N (= 21) from accumulator
      %% Subtract 21 from accumulator
      \\

      000E &
      \color{KwdColor}0A\color{IdentifierColor}0005 &
      \color{KwdColor}JNEG & \color{IdentifierColor}0005 &
      \tiny
Jump if accumulator is negative
      \\

    \end{tabular}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Labels}

  \begin{center}
    \scriptsize
    \begin{tabular}{rlll}
                              &   & \color{KwdColor}ORG & 100 \\      
      \textcolor{KwdColor}{N} & : & \color{KwdColor}EQU & 21 \\      
      \textcolor{KwdColor}{A} & : & \color{KwdColor}DS  & 1 \\
      \textcolor{KwdColor}{B} & : & \color{KwdColor}DS  & 1 \\
      \textcolor{KwdColor}{I} & : & \color{KwdColor}DS  & 1 \\
    \end{tabular}

    \small
    \vspace{0.5\baselineskip}
    \begin{tabular}{ll|ll|l}
      \bf
Address
      &
      \bf
Machine code
      &
      \multicolumn{2}{l|}{\bf
Mnemonics
      } &
      \bf
Meaning
      \\
      \hline

      &
      \color{KwdColor}02\color{IdentifierColor}0001 &
      \color{KwdColor}LDC & \color{IdentifierColor}1 &
      \tiny
Load const. 1 into the accumulator
      \\

      &
      \color{KwdColor}04\color{IdentifierColor}0000 &
      \color{KwdColor}ST & \color{IdentifierColor}A &
      \tiny
Store acc. to mem. location A
      \\

      &
      \color{KwdColor}04\color{IdentifierColor}0001 &
      \color{KwdColor}ST & \color{IdentifierColor}B\\

      &
      \color{KwdColor}02\color{IdentifierColor}0000 &
      \color{KwdColor}LDC & \color{IdentifierColor}0\\

      &
      \color{KwdColor}04\color{IdentifierColor}0002 &
      \color{KwdColor}ST & \color{IdentifierColor}I\\

      \textcolor{IdentifierColor}{LOOP}: &
      \color{KwdColor}03\color{IdentifierColor}0001 &
      \color{KwdColor}LD & \color{IdentifierColor}B &
      \tiny
Load acc. from mem. location B
      \\

      &
      \color{KwdColor}0B\color{IdentifierColor}0000 &
      \color{KwdColor}ADD & \color{IdentifierColor}A &
      \tiny
Add contents of location A to acc.
      \\

      ... \\

      
      &
      \color{KwdColor}1A\color{IdentifierColor}0015 &
      \color{KwdColor}SUBC & \color{IdentifierColor}N &
      \tiny
Subtract N (= 21) from accumulator
      \\

      &
      \color{KwdColor}0A\color{IdentifierColor}0005 &
      \color{KwdColor}JNEG & \color{IdentifierColor}LOOP &
      \tiny
Jump if accumulator is negative
      \\

    \end{tabular}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Assembler program}

  \begin{center}
    \small
    \begin{tabular}{l|ll|l}
      \bf
Label
      &
      \bf
Operation
      &
      \bf
Operand(s)
      &
      \bf
Comment
      \\
      \hline

                               & \color{KwdColor}ORG & 100 \\
      \textcolor{KwdColor}{N}: & \color{KwdColor}EQU & 21 &
      \tiny
; Number of sequence members to compute
      \\
      \textcolor{KwdColor}{A}: & \color{KwdColor}DS  & 1 \\
      \textcolor{KwdColor}{B}: & \color{KwdColor}DS  & 1 \\
      \textcolor{KwdColor}{I}: & \color{KwdColor}DS  & 1 \\

      &&&\\
      &
      \color{KwdColor}LDC & \color{IdentifierColor}1 &
      \tiny
; Load const. 1 into accumulator
      \\

      &
      \color{KwdColor}ST & \color{IdentifierColor}A &
      \tiny
; Store acc. to mem. location A
      \\

      &
      \color{KwdColor}ST & \color{IdentifierColor}B\\

      &
      \color{KwdColor}LDC & \color{IdentifierColor}0\\

      &
      \color{KwdColor}ST & \color{IdentifierColor}I\\

      \textcolor{IdentifierColor}{LOOP}: &
      \color{KwdColor}LD & \color{IdentifierColor}B &
      \tiny
; Load acc. from mem. location B
      \\

      &
      \color{KwdColor}ADD & \color{IdentifierColor}A &
      \tiny
; Add contents of location A to acc.
      \\

      ... &&& \\

      
      &
      \color{KwdColor}SUBC & \color{IdentifierColor}N &
      \tiny
; Subtract 21 from accumulator
      \\

      &
      \color{KwdColor}JNEG & \color{IdentifierColor}LOOP &
      \tiny
; Repeat the loop if acc. is negative
      \\

    \end{tabular}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Addressing modes}

  \begin{center}
    \begin{tabular}{l|l|p{0.4\textwidth}}
      \textbf{
Mode
      } & \textbf{
Action
      } & \textbf{
Description
      } \\
      \hline

Immediate
      & A $\leftarrow$ operand &
      {
        \raggedright\footnotesize
Use value that is in the command itself
      }
      \\

Direct
      & A $\leftarrow$ [operand] &
      {
        \raggedright\footnotesize
Use value using \textit{address} in the command
      }
      \\

Register
      & A $\leftarrow$ IDX &
      {
        \raggedright\footnotesize
The value used is in a register
      }
      \\

Indirect
      & A $\leftarrow$ [IDX] &
      {
        \raggedright\footnotesize
\textit{Address} of the value is in a register
      }
      \\

Indexed
      & \footnotesize A $\leftarrow$ operand[IDX] &
      {
        \raggedright\footnotesize
Address of the value is the register content + the operand
      }
      \\

    \end{tabular}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{CPU model visible to programmer}

  %%BEGIN LANGUAGE en
  From the point of view of a programmer, to describe a processor we
  need to specify:
  %%END LANGUAGE en



  \begin{itemize}
  \item
    %%BEGIN LANGUAGE en
    The \textbf{CPU registers} that are visible to the programmer
    %%END LANGUAGE en



  \item    
    %%BEGIN LANGUAGE en
    The \textbf{Instruction set architecture (ISA)} of the CPU
    %%END LANGUAGE en



  \item
    %%BEGIN LANGUAGE en
    The \textbf{addressing modes} of the CPU
    %%END LANGUAGE en



  \item
    %%BEGIN LANGUAGE en
    The \textbf{memory organisation} of the CPU
    %%END LANGUAGE en


    
  \item
    %%BEGIN LANGUAGE en
    The \textbf{input/output} of the CPU
    %%END LANGUAGE en


    
  \end{itemize}

  \rightline{\scriptsize\mycite{GutierrezOsuna2000}}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{16-bit Harvard RISC-like CPU}

  \begin{itemize}
  \item
Registers: 
    \texttt{A, PC, IDX}

  \item 
Instruction format: 
    \fbox{OPC (8
bit
      )}\fbox{ADDR/IMM (16
bit
      )}

  \item
Word size: 

    \begin{itemize}
    \item
data word –
      16
bits

    \item
instruction word –
      24 ($\text{8}+\text{16}$)
bits

    \end{itemize}
    
  \item
Address width:
    16
bits
    
  \item
Addressable unit: word
    
  \item
Address space: flat
    
  \item
ISA:
accumulator architecture;
    \texttt{LD, LDC, ST, ADD, JNZ, JNEG, ...}
    
  \item
    %%BEGIN LANGUAGE en
    Input/output: ports in separate address space; no interrupts; one
    input and one output port
    %%END LANGUAGE en
    
  \end{itemize}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Registers}

  \begin{center}
    Intel 8086 CPU
    
    \includegraphics[width=4cm]{drawings/Intel/8086-regs.eps}
  \end{center}

  \rightline{\scriptsize\mycite{INTEL1979}}

\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Segmented addressing}

  %%BEGIN LANGUAGE en
  Segment address is shifted by 4 bits and added to the offset:
  %%END LANGUAGE en

  \vspace{\baselineskip}
  \begin{center}
    \includegraphics[width=3cm]{drawings/Intel/8086-segments.eps}
  \end{center}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}
\frametitle{Take home messages}

  \begin{itemize}
  \item
    %%BEGIN LANGUAGE en
    Machine code is binary (for binary machines), usually expressed in HEX
    %%END LANGUAGE en
  \item
    %%BEGIN LANGUAGE en
    Command mnemonics are used to facilitate instruction description
    and memorisation
    %%END LANGUAGE en
  \item
    %%BEGIN LANGUAGE en
    Assemblers are used to facilitate programming in machine language
    %%END LANGUAGE en
  \item
    %%BEGIN LANGUAGE en
    Main notions of assembler: labels, operations, operands,
    pseudoinstructions, comments
    %%END LANGUAGE en
  \item
    %%BEGIN LANGUAGE en
    Computer/CPU architecture can be described by specifying several
    programmer-visible parameters
    %%END LANGUAGE en
  \end{itemize}
  
\end{frame}

%------------------------------------------------------------------------------

\begin{frame}%%[allowframebreaks]
\frametitle{References}

  \renewcommand{\bibfont}{\scriptsize}
  \printbibliography

\end{frame}

%------------------------------------------------------------------------------                                                                                                          
\end{document}
% 2021-10-18 09:52:37 EEST
