0% found this document useful (0 votes)
7 views3 pages

Lab/Ps 1: Elec 230: Programming For Engineers Spring 2026 Instructors: Tas

The document outlines the lab assignment for ELEC 230: Programming for Engineers, focusing on using the Vim editor and the gcc compiler. It includes step-by-step instructions for creating directories, editing files, and compiling a C program through various stages: preprocessing, compilation, assembly, and linking. Additionally, it emphasizes the importance of understanding Vim commands and the gcc process for effective programming.

Uploaded by

zanahmet472134
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Lab/Ps 1: Elec 230: Programming For Engineers Spring 2026 Instructors: Tas

The document outlines the lab assignment for ELEC 230: Programming for Engineers, focusing on using the Vim editor and the gcc compiler. It includes step-by-step instructions for creating directories, editing files, and compiling a C program through various stages: preprocessing, compilation, assembly, and linking. Additionally, it emphasizes the importance of understanding Vim commands and the gcc process for effective programming.

Uploaded by

zanahmet472134
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ELEC 230: Programming for Engineers

Spring 2026
Instructors: Zafer Doğan
TAs: Onat Üre, Eser İlke Genç, M. Oğuzhan Gültekin, Samet Demir

Lab/PS 1
23 February 2026

1. Getting familiar with Vim

(o) Open a terminal window and review the Basic Terminal Commands of PS0.
TODO:Create a new directory named PS1 in your home directory,
mkdir ∼/ps1
and place the downloaded text file [Link] into PS1. Go inside the directory PS1.
cd ∼/PS1

(a) Launch Vim Editor:


In order to launch Vim open a terminal, and type the command vim. TODO: Open the file by
specifying a name:
vim [Link]
If [Link] exists, it will be edited. Otherwise it will be created.

(b) Insert text:(review the Basic Vim Commands in PS0)


By default, when Vim starts, you cannot simply type to enter text because Vim starts in normal
mode (aka command mode). While confusing for new users, normal mode provides the power of
Vim because typing a few keys can perform many useful functions.
Command mode: In command (normal) mode, you can enter commands, for example, to copy,
delete or indent text. You return to normal mode from other modes by pressing the Esc key.
Insert mode: You can enter insert mode from normal mode by pressing the i key. Now, text
you type will be inserted.
Remark: Vim has other modes, like Visual, Select, and Ex-Mode, but Command and Insert
modes are good enough for us.
TODO: Move the curser the end of the second world and append the abbreviation (KU).
There are multiple ways to it. One naive way is to go into insert mode directly and move the
cursor with the arrows and then insert your text.
i - insert before the cursor
A better alternative would be to jump to the end of the second word and then append
e - jump forwards to the end of a word
a - insert (append) after the cursor Tip: Prefix a cursor movement command with a number
to repeat it. TODO: Go to the line with ’College of Engineering (CE)’ and change the word
’College’ into ’Faculty’
Again there are multiple ways to it. One way to do so, first go to line 8, delete the first word,
go into insert mode and add the text.
gg - go to the first line of the document
dw - delete (cut) the characters of the word from the cursor position
TODO: Copy 4 lines starting from line 5, and paste it at the end of the file.
yy - yank (copy) a line
$ - jump to the end of the line
p - put (paste) the clipboard after cursor
TODO: Delete the line before the last line.
G - go to the last line of the document
dd - delete (cut) a line

(c) Exiting the vim


:wq write (save) and quit [Most used one]
:w - write (save) the file, but don’t exit
:w! - enforce write out the current file using sudo
:q - quit (fails if there are unsaved changes)
:q! - quit and throw away unsaved changes

(d) Viminfo session: The viminfo file is used to store multiple information. First observe the details:
vim ∼/.viminfo
Note that in some cases, you may see the following error message:
Found a swap file by the name “∼/.[Link]”.
either due to multiple open terminals or due to some other problem. Closing multiple terminals
should solve the issue. If not, you may see the following option:

Swap file “.[Link]” already exists!


(O)pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it:
Be careful with the option you choose. Usually, the (R) should work fine.
If you observe no such problem, get the copy of viminfo file as follows:
vim ∼/.viminfo > [Link]

2. Getting familiar with gcc In this exercise, we will go over the four stages of compiling a C
program with gcc compiler. First, write the following simple C program in the standard environment
and save it as ps1.c

(a) First stage is the preprocessing


- removing the comments from the source code.
- preprocessing macro expansion.
- expansion of header files
When you preprocess a c source file, it will generate a file with an ‘.i’ extension by default.
You can do this by using the following command:
gcc -E ps1.c -o ps1.i (the result is redirected to the standard output, if no output filename is
provided)
Observe the changes after this step: vim ps1.i
(b) Second step is the Compilation
It takes in the temporary file from the preprocessing stage (with the .i extension).
It translates the file into assembly language.
It also checks the C language syntax for errors.
When you compile it, it will generate a file with the extension .s and the command line for
it is:
gcc -S ps1.i -o ps1.s
Observe the changes after this step: vim ps1.s
(c) Third step is the Assembler
It will take in the last code (.s extension file from compilation) and will translate it into
low-level machine code, i.e., a binary file.
It will then generate a file with a .o extension with:
gcc -c ps1.s -o ps1.o
Note that Vim editor cannot be used to open a binary file. Instead, to observe the file, use
the following
od -h ps1.o
(d) Last step is the Linker
It will take in the .o extension file that was generated by the assembler.
It will link the functions with their original definition so that the function printf() gets linked
to its original definition.
Then it will generate the executable file with
gcc ps1.o -o ps1n
To observe the file, again use the following
od -h ps1n
(e) Update the source code of same program without changing the preprocessor macro definitions
and save it as ps1n.c so that it will have the following output:

Note that you should define a CIRCLE AREA macro to compute the area of the unit circle
and use it properly.

You might also like