diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
| commit | 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch) | |
| tree | ed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/homework-one | |
| download | misc-undergrad-main.tar.gz misc-undergrad-main.zip | |
Diffstat (limited to 'Homework/cs5300/homework-one')
| -rw-r--r-- | Homework/cs5300/homework-one/compilers-one.org | 137 | ||||
| -rw-r--r-- | Homework/cs5300/homework-one/compilers-one.pdf | bin | 0 -> 147645 bytes | |||
| -rw-r--r-- | Homework/cs5300/homework-one/compilers-one.tex | 191 | ||||
| -rw-r--r-- | Homework/cs5300/homework-one/hw-ch1-src-1.zip | bin | 0 -> 17921 bytes | |||
| -rw-r--r-- | Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf | bin | 0 -> 16181 bytes | |||
| -rw-r--r-- | Homework/cs5300/homework-one/hw-ch1-src/common.tex | 52 | ||||
| -rw-r--r-- | Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex | 196 |
7 files changed, 576 insertions, 0 deletions
diff --git a/Homework/cs5300/homework-one/compilers-one.org b/Homework/cs5300/homework-one/compilers-one.org new file mode 100644 index 0000000..a723cd8 --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.org @@ -0,0 +1,137 @@ +#+TITLE: Homework - Chapter One +#+AUTHOR: Logan Hunt +#+OPTIONS: toc:nil +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \noindent \notag \usepackage{ dsfont } + +* Question One +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. + +* Question Two +** a +Compiled machine code is typically much faster than interpreted instructions. +** b +An interpreter can give better error diagnostics. + +* Question Three +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. + +* Question Four +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. + +* Question Five +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. + +* Question Six +\begin{verbatim} +R1 = 20 +R2 = 12 +\end{verbatim} + +* Question Seven +id2 is not a register + +* Question Eight +\begin{verbatim} +1. R1 = 9 +2. R2 = 2 +3. R1 = 11 +4. R1 = 6.0 +5. id3 = R1 = 6.0 +\end{verbatim} + +* Question Nine +** JavaScript ++ imperative ++ declarative ++ third-generation ++ object-oriented (through prototypes) ++ functional ++ scripting +** Python ++ imperative ++ declarative ++ third-generation ++ object-oriented ++ functional ++ scripting + +* Question Ten +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. + +* Question Eleven +1. Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably + be a nightmare to fix. +2. Anything in the Linux kernel that runs in user space. +3. ~malloc~ + +* Question Twelve +This contrived program: +#+BEGIN_SRC C + int main() { + int i; + for (i = 0; i < 10000000; i++); + return 0; + } +#+END_SRC + +It wouldn't be so bad for the compiler to just set i = 10000000. + +* Question Thirteen +| Declaration | Scope | +| ~int b = 1~ | B_1 - B_2 | +| ~int a = 2~ | B_2 - B_4 | +| ~int b = 2~ | B_2 - B_3 - B_4 | +| ~int b = 3~ | B_3 | +| ~int a = 4~ | B_4 - B_5 | +| ~int b = 4~ | B_4 | +| ~int a = 5~ | B_5 | + +* Question Fourteen +\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, ~w = 5, x = 4, y = 5, z = 6~. + +* Question Fifteen +\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, ~w = 5, x = 7, y = 16, z = 11~ + +* Question Sixteen +b : x = 4 \Rightarrow x = (x+3)-1 \Rightarrow x = 6 + +c : x = 1 \Rightarrow (x + 3) \Rightarrow 4 + +"6,4" diff --git a/Homework/cs5300/homework-one/compilers-one.pdf b/Homework/cs5300/homework-one/compilers-one.pdf Binary files differnew file mode 100644 index 0000000..f650f17 --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.pdf diff --git a/Homework/cs5300/homework-one/compilers-one.tex b/Homework/cs5300/homework-one/compilers-one.tex new file mode 100644 index 0000000..223758a --- /dev/null +++ b/Homework/cs5300/homework-one/compilers-one.tex @@ -0,0 +1,191 @@ +% 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}
\ No newline at end of file diff --git a/Homework/cs5300/homework-one/hw-ch1-src-1.zip b/Homework/cs5300/homework-one/hw-ch1-src-1.zip Binary files differnew file mode 100644 index 0000000..c8bbce3 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src-1.zip diff --git a/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf b/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf Binary files differnew file mode 100644 index 0000000..6f70e51 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/blocks.pdf diff --git a/Homework/cs5300/homework-one/hw-ch1-src/common.tex b/Homework/cs5300/homework-one/hw-ch1-src/common.tex new file mode 100644 index 0000000..81550ab --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/common.tex @@ -0,0 +1,52 @@ +\usepackage{version} + +\usepackage{makeidx} % allows index generation +\usepackage{graphicx} % standard LaTeX graphics tool + % when including figure files +\usepackage{subfigure} +\usepackage{multicol} % used for the two-column index +\usepackage[bottom]{footmisc}% places footnotes at page bottom +\usepackage{amstext,amssymb,amsmath} % I included these - AKG +\usepackage{url} +\usepackage{color} +\usepackage{multirow} +\usepackage{booktabs} + +% example use of listings (font is fixed-width): +% \begin{lstlisting}[mathescape,basicstyle=\fontfamily{lmvtt}\selectfont] +\usepackage{listings} +\lstset{basicstyle=\ttfamily} + +%------------------------------------------------------------------------------- +% Decrease margins +%------------------------------------------------------------------------------- +\oddsidemargin0cm +\topmargin-2cm +\textwidth16.5cm +\textheight23.5cm + +%------------------------------------------------------------------------------- +% this makes list spacing much better. +%------------------------------------------------------------------------------- +\newenvironment{tightenumerate}{ +\begin{enumerate} + \setlength{\itemsep}{1pt} + \setlength{\parskip}{0pt} + \setlength{\parsep}{0pt} +}{\end{enumerate} +} + +%------------------------------------------------------------------------------- +% this makes list spacing much better. +%------------------------------------------------------------------------------- +\newenvironment{tightitemize}{ +\begin{itemize} + \setlength{\itemsep}{1pt} + \setlength{\parskip}{0pt} + \setlength{\parsep}{0pt} +}{\end{itemize} +} + +\newcommand{\myspace}{\vspace{4 mm}} +\newcommand{\soln}{\paragraph{Solution:}} + diff --git a/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex b/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex new file mode 100644 index 0000000..7a1eed3 --- /dev/null +++ b/Homework/cs5300/homework-one/hw-ch1-src/hw-ch1-src.tex @@ -0,0 +1,196 @@ +\documentclass{article} + +\input{common} + +\renewcommand{\tt}[1]{\texttt{#1}} +\newcommand{\kc}{$^*$} +\renewcommand{\not}{$^\wedge$} +\newcommand{\pc}{$^+$} +\newcommand{\e}{$\epsilon$} + +\includeversion{version:soln} + +\begin{document} + +\begin{flushleft} +Homework - Chapter 1 \\ +CS 5300 %\\ + +\end{flushleft} + +\begin{enumerate} +\item (6 points) What is the difference between a compiler and an interpreter? + \begin{version:soln} + \soln Put your solution here. + \end{version:soln} + +\myspace +\item (6 points) According to the book, what are the advantages of + \begin{tightenumerate} + \item a compiler over an interpreter? + \item an interpreter over a compiler? + \end{tightenumerate} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Discuss the portability across computers of compiled vs. interpreted programs. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Strictly speaking, Java is an interpreted language. Discuss how it has elements of being a compiled language. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) The C compiler \textit{gcc} has an \tt{-S} option that will output assembly code before the assembler is run. Why might someone want to do this? Give at least two reasons. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) If id2 has a value of 8 and id3 has a value of 12, after running the following code, what will be in registers R1 and R2? +\begin{lstlisting} +LDF R1, id2 +LDF R2, id3 +ADDF R1, R1, R2 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) The assembler will fail on this assembly code. Why? +\begin{lstlisting} +LDF R1, id2 +LDF R2, id3 +ADDF R1, id2, R2 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) If id5 is 2 and id7 is 9, what will be in id3 after running this code? +\begin{lstlisting} +LDF R1, id7 +LDF R2, id5 +ADDF R1, R1, R2 +MULF R1, R2, #3.0 +STF id3, R1 +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Which of the following terms apply to Javascript? Which apply to Python? \\ +a)~imperative b)~declarative c)~von Neumann d)~object-oriented e)~third-generation f)~fourth-generation g)~scripting + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (5 points) C is not considered an object-oriented language, yet it has structs, which are kind of like objects. Why is it not object-oriented? + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) Give three examples of programs for which optimization that changes the behavior of the program would be very, very bad. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (5 points) Give an example of a program for which optimization that changes the behavior of the program might not be so bad. + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (8 points) Given the following blocks in a C++ program, create a table like figure 1.11 in the textbook. + +\includegraphics[width=.4\textwidth]{blocks.pdf} + +\begin{tabular}{c|c} +\toprule +Declaration & Scope \\ +\midrule +\tt{int b = 1;} & \\ +\tt{int a = 2;} & \\ +\tt{int b = 2;} & \\ +\tt{int b = 3;} & \\ +\tt{int a = 4;} & \\ +\tt{int b = 4;} & \\ +\tt{int a = 5;} & \\ +\bottomrule +\end{tabular} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +%\newpage +\item (8 points) For the following block-structured C code, indicate the values assigned to $w, x, y,$ and $z$. +\begin{lstlisting}[language=C] +int w, x, y, z; +int i = 5; +int j = 8; +{ int i = 4; + j = 9; + w = j - i; +} +x = j - i; +{ int j = 10; + y = j - i; + i = 3; +} +z = j - i; +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\newpage +\item (8 points) For the following block-structured C code, indicate the values assigned to $w, x, y,$ and $z$. +\begin{lstlisting}[language=C] +int w, x, y, z; +int i = 2; +int j = 5; +{ int i = 3; + w = i + j; +} +x = i + j; +{ int j = 9; + i = 7; + y = i + j; + { i = 6; + } +} +z = i + j; +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\myspace +\item (6 points) What is printed by the following C code? +\begin{lstlisting}[language=C] +#define a (x+3) +int x = 4; +void b() { x = a-1; printf("%d,", x); } +void c() { int x = 1; printf("%d\n", a); } +void main() { b(); c(); } +\end{lstlisting} + \begin{version:soln} + \soln + \end{version:soln} + +\end{enumerate} +\end{document} |
