Difference between revisions of "CSC231 Lab 4: Debugger"
(Created page with '--~~~~ ---- <meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thieba…') |
(→Running the Debugger) |
||
(12 intermediate revisions by the same user not shown) | |||
Line 42: | Line 42: | ||
nasm -f elf -F stabs simpleAdd.asm | nasm -f elf -F stabs simpleAdd.asm | ||
− | ld -o simpleAdd simpleAdd.o | + | ld -melf_i386 -o simpleAdd simpleAdd.o |
+ | =Test= | ||
− | + | Run your program. | |
+ | |||
+ | What happens? | ||
− | + | Why? | |
=The Debugger= | =The Debugger= | ||
Line 81: | Line 84: | ||
# Set a '''breakpoint''' on the second NOP instruction by clicking right on the instruction | # Set a '''breakpoint''' on the second NOP instruction by clicking right on the instruction | ||
# Display the 3 variables: '''Data''', then '''Memory''', then select to display 1 hex double-word at location '''&a'''. Same thing for '''b''' and '''result'''. | # Display the 3 variables: '''Data''', then '''Memory''', then select to display 1 hex double-word at location '''&a'''. Same thing for '''b''' and '''result'''. | ||
+ | # Run the program by clicking '''Run''' in the tall grey window pane. The debugger will execute the first NOP and stop on the second one. | ||
# Single step the program with '''Stepi'''. | # Single step the program with '''Stepi'''. | ||
Line 98: | Line 102: | ||
Debug your program and show your instructor that you end up with 1, 1, 2, 3, 5, 8, 13 in memory. | Debug your program and show your instructor that you end up with 1, 1, 2, 3, 5, 8, 13 in memory. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | =Quiz= | ||
+ | |||
+ | * Create a new program in your account called la4.asm, with the code below. You can either use emacs and copy/paste the code, or use '''getcopy''', as follows | ||
+ | |||
+ | |||
+ | getcopy lab4.asm | ||
+ | |||
+ | |||
+ | <code><pre> | ||
+ | ;;; lab4.asm | ||
+ | ;;; YourName | ||
+ | ;;; | ||
+ | ;;; this program should be assembled, linked, and debugged | ||
+ | ;;; with ddd to figure out the contents of msg1 at the different | ||
+ | ;;; phases of the program: phase1, phase2, phase3, phase4, | ||
+ | ;;; and phase5 | ||
+ | ;;; | ||
+ | ;;; to assemble and run: | ||
+ | ;;; | ||
+ | ;;; nasm -f elf -F stabs lab4_mystery.asm | ||
+ | ;;; ld -melf_i386 -o lab4_mystery lab4_mystery.o | ||
+ | ;;; ./lab4_mystery | ||
+ | ;;; ------------------------------------------------------------------- | ||
+ | EXIT equ 1 | ||
+ | WRITE equ 4 | ||
+ | STDOUT equ 1 | ||
+ | |||
+ | ;; ------------------------------------------------------------ | ||
+ | ;; data areas | ||
+ | ;; ------------------------------------------------------------ | ||
+ | |||
+ | section .data | ||
+ | msg0 db 10, 10, 10, 10, 10 | ||
+ | msg1 db " " | ||
+ | db 10, 10, 10, 10, 10 | ||
+ | MSGLEN equ $-msg0 | ||
+ | |||
+ | ;; ------------------------------------------------------------ | ||
+ | ;; code area | ||
+ | ;; ------------------------------------------------------------ | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | |||
+ | _start: | ||
+ | nop | ||
+ | nop | ||
+ | nop | ||
+ | phase1: | ||
+ | mov eax, 0x6c6c6468 | ||
+ | mov ebx, 0x6064216e | ||
+ | mov ecx, 0x6c687400 | ||
+ | mov edx, 0x13070e09 | ||
+ | |||
+ | phase2: | ||
+ | mov dword [msg1], eax | ||
+ | mov dword [msg1+4], ebx | ||
+ | mov dword [msg1+8], ecx | ||
+ | mov dword [msg1+12], edx | ||
+ | |||
+ | phase3: or dword [msg1], 256 | ||
+ | xor dword [msg1+4], 0x01010101 | ||
+ | or dword [msg1+8], 'r' | ||
+ | phase4: | ||
+ | or word [msg1+12], 0x6060 | ||
+ | or word [msg1+14], 0x6060 | ||
+ | |||
+ | phase5: | ||
+ | mov eax, WRITE | ||
+ | mov ebx, STDOUT | ||
+ | mov ecx, msg0 | ||
+ | mov edx, MSGLEN | ||
+ | int 0x80 | ||
+ | |||
+ | |||
+ | ;; exit() | ||
+ | |||
+ | mov eax,EXIT | ||
+ | mov ebx, 0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
+ | </pre></code> | ||
+ | |||
+ | * Assemble, link, run and/or debug the program with ddd and answer the following questions on a sheet of paper. | ||
+ | |||
+ | |||
+ | ;Question 1 | ||
+ | : How many spaces are in msg1 (do not count, use ddd to answer the question!) | ||
+ | |||
+ | |||
+ | ;Question 2 | ||
+ | : what is the contents of the 4 data registers (eax, ebx, ecx, and edx) in '''decimal''' at the beginning of phase2? | ||
+ | |||
+ | |||
+ | ;Question 3 | ||
+ | : Same question, but this time express the contents of the registers in '''Ascii''' (You may want to use this [http://www.asciitable.com/ Ascii table] to answer this part). | ||
+ | |||
+ | |||
+ | ;Question 4 | ||
+ | : what is the contents of msg0 in memory, at the beginning of Phase3 (before Phase3 starts), in '''hex'''? | ||
+ | |||
+ | |||
+ | ;Question 5 | ||
+ | : Same question, but this time express msg0 in '''ascii'''? | ||
+ | |||
+ | |||
+ | ;Question 6 | ||
+ | : What is the purpose of the '''or''' at line 54, in terms of what it accomplishes toward the final goal of the program? | ||
+ | |||
+ | |||
+ | ;Question 7 | ||
+ | : What is accomplished by the two '''or''' instructions on Lines 56 and 57, in terms of modifying the string msg1? | ||
+ | |||
+ | |||
+ | ;Question 8 | ||
+ | : What gets printed by the program? | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Labs]] |
Latest revision as of 08:26, 24 September 2012
--D. Thiebaut 14:03, 24 September 2010 (UTC)
<meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thiebaut's Web Page" /> <meta name="title" content="Dominique Thiebaut -- Computer Science" /> <meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" /> <meta name="author" content="thiebaut at cs.smith.edu" /> <meta name="distribution" content="Global" /> <meta name="revisit-after" content="10 days" /> <meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" /> <meta name="robots" content="FOLLOW,INDEX" />
Contents
This lab will introduce you to the ddd debugger, and simple steps you can take to debug assembly programs.
The Target Program
Create a simple program that adds the contents of two variables and stores the result in a third one.
The data section should be something like this:
a dd 3 b dd 5 result dd 0
The code section, something like this:
nop nop mov eax, dword[a] add eax, dword[b] mov dword[result], eax
Save your program as simpleAdd.asm, and generate the executable as follows:
nasm -f elf -F stabs simpleAdd.asm ld -melf_i386 -o simpleAdd simpleAdd.o
Test
Run your program.
What happens?
Why?
The Debugger
Start the Debugger
Start the debugger and instruct it to open the program simpleAdd
ddd simpleAdd &
Configuration
You need to do this step only once, the very first time you use the debugger.
Before we can start using DDD, we need to change some of its default settings.
Select "Edit->Preferences" from the main menu. On the window that will appear, open the "Source" tab (top of the window). Then, check the "Display Source Line Numbers" option. Click OK to close the window.
Next, select "Edit->GDB Settings" from the main menu. In the window that pops up, scroll approximately half way down through the list of options, until you find the option "Disassembly flavor". Change its value to "Intel" (Figure 3.2) and click on CLOSE.
Running the Debugger
Just follow the same steps we did in class to execute your program one step at a time, and to verify that the number 8 ends up in the variable result.
- Click on Status, Registers to see the register window
- Set a breakpoint on the second NOP instruction by clicking right on the instruction
- Display the 3 variables: Data, then Memory, then select to display 1 hex double-word at location &a. Same thing for b and result.
- Run the program by clicking Run in the tall grey window pane. The debugger will execute the first NOP and stop on the second one.
- Single step the program with Stepi.
Exercise 1
Edit your program so that now it computes the sum of 5 variables, a, b, c, d, and e, and saves the sum in result.
Single step your program and verify that it generates the correct answer.
Exercise 2
Modify your program one more time and make the data section equal to this definition:
Fib dd 1, 1, 0, 0, 0, 0, 0
And modify the code so that your program will store the sum of the first two double-words into the third double-word, the sum of the second and third double-word in the fourth, and so on. This way your program will compute the first 7 terms of the Fibonacci sequence.
Debug your program and show your instructor that you end up with 1, 1, 2, 3, 5, 8, 13 in memory.
Quiz
- Create a new program in your account called la4.asm, with the code below. You can either use emacs and copy/paste the code, or use getcopy, as follows
getcopy lab4.asm
;;; lab4.asm
;;; YourName
;;;
;;; this program should be assembled, linked, and debugged
;;; with ddd to figure out the contents of msg1 at the different
;;; phases of the program: phase1, phase2, phase3, phase4,
;;; and phase5
;;;
;;; to assemble and run:
;;;
;;; nasm -f elf -F stabs lab4_mystery.asm
;;; ld -melf_i386 -o lab4_mystery lab4_mystery.o
;;; ./lab4_mystery
;;; -------------------------------------------------------------------
EXIT equ 1
WRITE equ 4
STDOUT equ 1
;; ------------------------------------------------------------
;; data areas
;; ------------------------------------------------------------
section .data
msg0 db 10, 10, 10, 10, 10
msg1 db " "
db 10, 10, 10, 10, 10
MSGLEN equ $-msg0
;; ------------------------------------------------------------
;; code area
;; ------------------------------------------------------------
section .text
global _start
_start:
nop
nop
nop
phase1:
mov eax, 0x6c6c6468
mov ebx, 0x6064216e
mov ecx, 0x6c687400
mov edx, 0x13070e09
phase2:
mov dword [msg1], eax
mov dword [msg1+4], ebx
mov dword [msg1+8], ecx
mov dword [msg1+12], edx
phase3: or dword [msg1], 256
xor dword [msg1+4], 0x01010101
or dword [msg1+8], 'r'
phase4:
or word [msg1+12], 0x6060
or word [msg1+14], 0x6060
phase5:
mov eax, WRITE
mov ebx, STDOUT
mov ecx, msg0
mov edx, MSGLEN
int 0x80
;; exit()
mov eax,EXIT
mov ebx, 0
int 0x80 ; final system call
- Assemble, link, run and/or debug the program with ddd and answer the following questions on a sheet of paper.
- Question 1
- How many spaces are in msg1 (do not count, use ddd to answer the question!)
- Question 2
- what is the contents of the 4 data registers (eax, ebx, ecx, and edx) in decimal at the beginning of phase2?
- Question 3
- Same question, but this time express the contents of the registers in Ascii (You may want to use this Ascii table to answer this part).
- Question 4
- what is the contents of msg0 in memory, at the beginning of Phase3 (before Phase3 starts), in hex?
- Question 5
- Same question, but this time express msg0 in ascii?
- Question 6
- What is the purpose of the or at line 54, in terms of what it accomplishes toward the final goal of the program?
- Question 7
- What is accomplished by the two or instructions on Lines 56 and 57, in terms of modifying the string msg1?
- Question 8
- What gets printed by the program?