0% found this document useful (0 votes)
36 views10 pages

CompOrg Tutorial 01

Uploaded by

air15902197881
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)
36 views10 pages

CompOrg Tutorial 01

Uploaded by

air15902197881
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
You are on page 1/ 10

Computer Organization and Programming – Winter 2024

Tutorial 1
Prof. Herman Schinca
Compiling and linking x86 ASM


Compiling an x86 ASM file:
gcc -c [asm_filename].s -o [object_filename].o
The -c is to compile or assemble the source files, but do not link. The linking stage simply is
not done.


Linking an x86 object file:
ld [object_filename].o -o [executable_filename] -e main
The “-e entry” specifies that the program entry point is “entry”.

2
Compiling and linking x86 ASM


Why do we need to put “-e entry” when we link?

This is because the main function in C is not actually the entry point of the program.
After compiling, the function _start calls main and then exits (using main’s return
value as exit code).

In your assembly code you can use _start instead of main, or you can use “-e entry”
as we did before.

3
Compiling and linking x86 ASM

Let’s modify the assembly code of hello-world.s to see if


the changes are applied after de compilation and linking process.

4
A disassembly tool: objdump


We can take an object file and translate the machine code (the 0’s and 1’s) into
assembly code. This inverse operation would probably result in a little bit different
program that the one that we wrote in ASM.


If we only want to see the .text part (the code):
objdump -d [object_filename]

5
A disassembly tool: objdump


Exercise 1: find the differences between doing:

objdump -d hello-world.o
and
objdump -d hello-world


Explain what happened.

6
A disassembly tool: objdump


We can also disassembly all the sections of the file.


If we want to see all the sections:
objdump -D [object_filename]

7
ASCII table to the rescue!

8
Compiling and disassembling hello-world.c


I made the same “hello world” program but in C.


Let’s compile it:
gcc -o hello-world hello-world.c


Exercise 2: using objdump, try to find the ASM part of the code that prints “hello,
world”.

9
Challenge of the day


Exercise 3: modify the program hello-world.s so that it could read the user name
and print on the screen “Hello [user_name]”.
Tip: use the syscall read and try to figure out how it could help you.

10

You might also like