% Created 2023-01-16 Mon 13:28 % Intended LaTeX compiler: pdflatex \documentclass[11pt]{article} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{graphicx} \usepackage{longtable} \usepackage{wrapfig} \usepackage{rotating} \usepackage[normalem]{ulem} \usepackage{amsmath} \usepackage{amssymb} \usepackage{capt-of} \usepackage{hyperref} \noindent \notag \usepackage{ dsfont } \author{Logan Hunt} \date{\today} \title{Homework - Chapter One} \hypersetup{ pdfauthor={Logan Hunt}, pdftitle={Homework - Chapter One}, pdfkeywords={}, pdfsubject={}, pdfcreator={Emacs 28.2 (Org mode 9.5.5)}, pdflang={English}} \begin{document} \maketitle \section{Question One} \label{sec:orge85a534} A compiler is a program that reads source in one language, and translates it to an equivalent program to another language, which can immediately be run in that language. An interpreter, on the other hand, directly executes the source as the program continues. \section{Question Two} \label{sec:orgb550d55} \subsection{a} \label{sec:org1f4ed7b} Compiled machine code is typically much faster than interpreted instructions. \subsection{b} \label{sec:orga3b0952} An interpreter can give better error diagnostics. \section{Question Three} \label{sec:orgdddbe9a} In terms of portability, compiled programs are less so than interpreters. Compiled programs (to machine code) target a single architecture, and thus require seperate compilation, or cross-compilation over all target architectures. Interpreted programs on the other hand will run anywhere, as long as there is an interpreter implementation for the architecture. \section{Question Four} \label{sec:org348f3da} Java is a shady devil that likes to play both sides. Java source is compiled to intermediate Java bytecodes, which are then interpreted by the Java Virtual Machine. The compilation aspect of Java here, is in the translation to JVM bytecode instructions. \section{Question Five} \label{sec:org2f098fc} One might want to view generated assembly code to debug an issue in their code and step through the compiler's output, understand any optimizations the compiler may make, or to just explore its output. \section{Question Six} \label{sec:org50a4a97} \begin{verbatim} R1 = 20 R2 = 12 \end{verbatim} \section{Question Seven} \label{sec:org199da87} id2 is not a register \section{Question Eight} \label{sec:org3216fe2} \begin{verbatim} 1. R1 = 9 2. R2 = 2 3. R1 = 11 4. R1 = 6.0 5. id3 = R1 = 6.0 \end{verbatim} \section{Question Nine} \label{sec:org2cf7709} \subsection{JavaScript} \label{sec:org769857f} \begin{itemize} \item imperative \item declarative \item third-generation \item object-oriented (through prototypes) \item functional \item scripting \end{itemize} \subsection{Python} \label{sec:orgb53d3f0} \begin{itemize} \item imperative \item declarative \item third-generation \item object-oriented \item functional \item scripting \end{itemize} \section{Question Ten} \label{sec:org6722ba8} According to the book, "A distinguishing feature of object-oriented programming is the ability of each object to invoke the appropriate method in response to a message." In C, this is not possible as structs do not have support for methods. \section{Question Eleven} \label{sec:orgc3a76ec} \begin{enumerate} \item Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably be a nightmare to fix. \item Anything in the Linux kernel that runs in user space. \item \texttt{malloc} \end{enumerate} \section{Question Twelve} \label{sec:org5f61a58} This contrived program: \begin{verbatim} int main() { int i; for (i = 0; i < 10000000; i++); return 0; } \end{verbatim} It wouldn't be so bad for the compiler to just set i = 10000000. \section{Question Thirteen} \label{sec:orgebfa92b} \begin{center} \begin{tabular}{ll} Declaration & Scope\\[0pt] \texttt{int b = 1} & B\textsubscript{1} - B\textsubscript{2}\\[0pt] \texttt{int a = 2} & B\textsubscript{2} - B\textsubscript{4}\\[0pt] \texttt{int b = 2} & B\textsubscript{2} - B\textsubscript{3} - B\textsubscript{4}\\[0pt] \texttt{int b = 3} & B\textsubscript{3}\\[0pt] \texttt{int a = 4} & B\textsubscript{4} - B\textsubscript{5}\\[0pt] \texttt{int b = 4} & B\textsubscript{4}\\[0pt] \texttt{int a = 5} & B\textsubscript{5}\\[0pt] \end{tabular} \end{center} \section{Question Fourteen} \label{sec:org5304edd} \begin{verbatim} i = 5 j = 8 i = 4[ j = 9 w = j - i = 9 - 4 = 5 x = j - i = 9 - 5 = 4 j = 10 y = j - i = 10 - 5 = 5 i = 3 z = j - i = 9 - 3 = 6 \end{verbatim} Thus, \texttt{w = 5, x = 4, y = 5, z = 6}. \section{Question Fifteen} \label{sec:org9f74779} \begin{verbatim} i = 2 j = 5 i = 3 w = i + j = 3 + 2 = 5 x = i + j = 2 + 5 = 7 j = 9 i = 7 y = i + j = 7 + 9 = 16 i = 6 z = i + j = 6 + 5 = 11 \end{verbatim} Thus, \texttt{w = 5, x = 7, y = 16, z = 11} \section{Question Sixteen} \label{sec:org31a90e4} b : x = 4 \(\Rightarrow\) x = (x+3)-1 \(\Rightarrow\) x = 6 c : x = 1 \(\Rightarrow\) (x + 3) \(\Rightarrow\) 4 "6,4" \end{document}