0% found this document useful (0 votes)
28 views8 pages

Islam CSC342342 TakeHomeTest 1

This document contains the answers to test questions about debugging a C program using GDB. It explains key GDB commands like info registers, x/i, x/g and how they are used to examine program state including register values, disassemble instructions, and print values on the stack.

Uploaded by

Mehedi Khan
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)
28 views8 pages

Islam CSC342342 TakeHomeTest 1

This document contains the answers to test questions about debugging a C program using GDB. It explains key GDB commands like info registers, x/i, x/g and how they are used to examine program state including register values, disassemble instructions, and print values on the stack.

Uploaded by

Mehedi Khan
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/ 8

CITY COLLEGE OF NEW YORK

CS342/343
Fall 2023
Instructor: Professor Izidor Gertner
TA: Albi Arapi

Take Home Test 1:


GDB/GNU

Submitted By
Md Wasiul Islam
Date: 11/07/2023
Take Home Test CSC342

Start Time and Date: 5:00pm, November 7

End Time and Date: 8:35pm, November 7

(1) We know, Program length (in bytes) = Ending Address – Starting Address

= 0x000000000040158e - 0x0000000000401550

= 0x3E
Converting hex to decimal: 3x16^1 + 15x16^0 = 63 bytes
Therefore, the program length is 63 bytes

(2) To find the length of each instruction we use,


Current address length = Next address – Current address

Based on the assembler code dump,


1 byte instructions: push, pop
Explanation: They are short, single-byte opcodes used for pushing and popping data from the
stack.
2 byte instructions: add
Explanation: It took 2 bytes when used to add two registers (e.g., %edx and %eax)
3 byte instructions: mov
Explanation: It took 3 bytes when it was used to move data between registers
4 byte instructions: sub, add
Explanation: “sub” took 4 bytes because it involved a 32-bit immediate value ($0x40) to adjust
the stack pointer (%rsp).
“add” took 4 bytes when used to add an immediate value to a register (e.g., add $0x40,%rsp)
because the encoding of the instruction is influenced by the specific operands and addressing
modes involved.
5 byte instructions: callq
Explanation: It took 5 bytes because it involves a 32-bit relative displacement for the target
address
6 byte instructions: No instructions took 6 bytes
7 byte instructions: movl
Explanation: It took 7 bytes because it includes the 5-byte ”mov” instruction for moving a 32-bit
immediate value, followed by an additional 2 bytes to encode the immediate value itself.

(3)
In my architecture, “info register rip” is the gdb command to print the content of the instruction
pointer register. The rip register contains the current value of the instruction pointer, which points
to the address of the currently executing instruction, 0x40155d.

(4)
In my architecture, “info register rbp” is the gdb command to print the content of the base pointer
register. The rbp register contains the current value of the base pointer, which points to the
address, 0x61fe20.
(5)
In my architecture, “info register rsp” is the gdb command to print the content of the stack pointer
register. The rsp register contains the current value of the stack pointer, which points to the
address, 0x61fde0.

(6)
In my architecture, “x/1wx $rbp-4” is the gdb command I used to print the content and the address
of 4-byte word on stack at a offset -4 from base pointer. Here, x is the examine command, /1
specifies that you want to print 1 unit of data, w specifies that the data is in word (4 bytes) format
and $rbp-4 is the memory address calculated as an offset from the base pointer (RBP) minus 4
bytes, which is the typical size of a word.

(7)
In my architecture, “info program” provides information about the program's execution state,
including the current instruction address. Here it shows that the program stopped at 0x40155d,
so this will be next address of instruction that will be executed.

(7.1)

In my architecture, “x/i {address}” is the gdb command to find the machine instruction. Here, “x/i”
allows to examine the machine instruction at the given address, in this case, movl $0xffffffff,-
0x4(%rbp), which is the instruction at the breakpoint.
(9)

In my architecture, “info register” in GDB is used to display the current values of all the general-
purpose and floating-point registers. Here we can see that rbp is pointing to 0x61fe20, which is the
value stored in the base pointer register.

(10) Using the same picture as (9), “info register” in GDB is used to display the current values of all the
general-purpose and floating-point registers. Here, we can see that rsp is pointing to 0x61fde0, which
is the value stored in the stack pointer register.

(11) In order to print out machine instructions at each address, we need to do “x/i <address>”. In the
address space we put in the corresponding address that is listed in our dump assembler code. Here,

In order to print out all the machine instructions at once without repeating the command over and
over again, I have done the following gdb commands,
Here I have set the address to the first address and the loop will stop once it reaches the end
address. Here are my screenshots for the machine instructions of all addresses.
(12) In order to print out the stack starting from the base pointer in gdb, we can use the following
command, “x/gx rbp-0x(some number)” Here in some number, we can put any value and it should
give us the address of a value on the stack based on the base pointer. For example:
I didn’t list everything because we just use similar commands for them.

(12.1) The local variable a is -0x4 in hexadecimal and in decimal it is 4. The offset from base pointer
to the local variable "a" is -0x4, which is the same in hexadecimal representation. So, the hex offset is
also -0x4. Therefore in decimal representation, the offset is -4.

You might also like