CSC231 Homwork 2 Solution 2010
--D. Thiebaut 21:35, 6 October 2010 (UTC)
Number Systems
Angela Zhu
231a-ab
hw2b.txt
CSC 231
Sept. 29, 2010
Decimal: 135
Hexadecimal: 87
Binary: 1000 0111
Decimal: 1001
Hexadecimal: 3E9
Binary: 0011 1110 1001
Decimal: 255
Hexadecimal: FF
Binary: 1111 1111
Decimal: 2561
Hexadecimal: A01
Binary: 1010 0000 0001
Decimal: 65535
Hexadecimal: FFFF
Binary: 1111 1111 1111 1111
Decimal: 63640
Hexadecimal: F898
Binary: 1111 1000 1001 1000
Decimal: 31
Hexadecimal: 1F
Binary: 1 1111
Decimal: 2290649224
Hexadecimal: 88888888
Binary: 1000 1000 1000 1000 1000 1000 1000 1000
Logic Design
- To test the validity of boolean function, a simple Python program can be of great help.
#! /bin/env python
# truthtable.py
# D. Thiebaut
# how a simple python program can generate the
# truth table of a boolean function
#
def xor( a, b ):
return a and (not b) or (not a ) and b
def candidate( b3, b2, b1, b0 ):
return b2 and b3 and not (b0 and b1) \
or b0 and b1 and not (b2 and b3) \
or xor( b0, b1) and xor( b2, b3 )
def main():
print " a b c d | f "
print "------------+------"
for a in [ 0, 1 ]:
for b in [ 0, 1 ]:
for c in [ 0, 1 ]:
for d in [ 0, 1 ]:
x = candidate( a, b, c, d )
print "%3d%3d%3d%3d |%3d" % \
( a, b, c, d, x )
main()
Mystery Program
- The program prints a quote attributed to Kurt Cobain: "Practice makes perfect... but nobody's perfect... so why practice?"
1 ;;; hw2c.asm
2 ;;; Alec Goebel
3 ;;;
4 ;;; reverse engineered from provided hex dump.
5 ;;; Prints "practice makes perfect...but nobody's perfect...so why practice?\n\n"
6 ;;;
7 ;;; to assemble:
8 ;;;
9 ;;; nasm -f elf -F stabs hw2c.asm
10 ;;; ld -o program hw2c.o
11 ;;;
12 ;;; to run
13 ;;; $ ./hw2c
14 ;;; practice makes perfect...but nobody's perfect...so why practice?
15 ;;;
16 ;;; $
17 ;;; -------------------------------------------------------------------
18
19 EXIT equ 1
20 WRITE equ 4
21 STDOUT equ 1
22
23 ;; ------------------------------------------------------------
24 ;; data areas
25 ;; ------------------------------------------------------------
26
27 section .data
28
29 00000000 206D616B657320 makes db " makes "
30 00000007 706572666563742E2E- prct db "perfect...practice"
31 00000010 2E7072616374696365
32 00000019 627574206E6F626F64- nbdy db "but nobody's so why "
33 00000022 79277320736F207768-
34 0000002B 7920
35 0000002D 3F0A0A qst db "?", 10, 10
36
37
38 ;; ------------------------------------------------------------
39 ;; code area
40 ;; ------------------------------------------------------------
41
42 section .text
43 global _start
44
45 _start:
46
47 ;; write 'practice' on STDOUT
48 00000000 B804000000 mov eax,WRITE
49 00000005 BB01000000 mov ebx,STDOUT
50 0000000A B9[11000000] mov ecx,prct+10
51 0000000F BA08000000 mov edx,8
52 00000014 CD80 int 0x80
53
54 ;; pointless instruction
55 00000016 B8FFFFFFFF mov eax,0xffffffff
56
57 ;; write ' makes ' on STDOUT
58 0000001B B804000000 mov eax,WRITE
59 00000020 BB01000000 mov ebx,STDOUT
60 00000025 B9[07000000] mov ecx,prct ;; pointless
61 0000002A B9[00000000] mov ecx,makes
62 0000002F BA07000000 mov edx,7
63 00000034 CD80 int 0x80
64
65 ;; write 'perfect...' on STDOUT
66 00000036 BA[11000000] mov edx,prct+10 ;; pointless
67 0000003B B801000000 mov eax,1 ;; pointless
68 00000040 B804000000 mov eax,WRITE
69 00000045 BB01000000 mov ebx,STDOUT
70 0000004A B9[07000000] mov ecx,prct
71 0000004F BA0A000000 mov edx,10
72 00000054 CD80 int 0x80
73
74 ;; write "but nobody's " on STDOUT
75 00000056 B804000000 mov eax,WRITE
76 0000005B BB01000000 mov ebx,STDOUT
77 00000060 B9[19000000] mov ecx,nbdy
78 00000065 BA0D000000 mov edx,13
79 0000006A CD80 int 0x80
80
81 ;; write "perfect..." to STDOUT
82 0000006C B804000000 mov eax,WRITE
83 00000071 BB01000000 mov ebx,STDOUT
84 00000076 B9[06000000] mov ecx,prct-1
85 0000007B BA0A000000 mov edx,10
86 00000080 41 inc ecx
87 00000081 CD80 int 0x80
88
89 ;; write "so why " to STDOUT
90 00000083 B804000000 mov eax,WRITE
91 00000088 BB01000000 mov ebx,STDOUT
92 0000008D B9[24000000] mov ecx,nbdy+11
93 00000092 41 inc ecx
94 00000093 BA07000000 mov edx,7
95 00000098 41 inc ecx
96 00000099 CD80 int 0x80
97
98 ;; write "practice" to STDOUT
99 0000009B B804000000 mov eax,WRITE
100 000000A0 BB01000000 mov ebx,STDOUT
101 000000A5 B9[11000000] mov ecx,prct+10
102 000000AA BA08000000 mov edx,8
103 000000AF CD80 int 0x80
104
105 ;; write "?\n\n"
106 000000B1 B804000000 mov eax,WRITE
107 000000B6 BB01000000 mov ebx,STDOUT
108 000000BB B9[2D000000] mov ecx,qst
109 000000C0 BA03000000 mov edx,3
110 000000C5 CD80 int 0x80
111
112 ;; exit()
113 000000C7 B801000000 mov eax,EXIT
114 000000CC BB00000000 mov ebx,0
115 000000D1 CD80 int 0x80 ; final system call
- To test the programs, the hexadecimal dump of the object file of the original program was compared to the hexadecimal dump of the object file of your version.
- Assume that hw2_mystery.solution.asm is the orginal that you have to recreate, and assume that hw2c.asm is your program:
nasm -f elf hw2c.asm hexdump -C hw2c.o | tail -190 | head -40 > hw2c.lst nasm -f elf hw2_mystery.solution.asm hexdump -C hw2_mystery.solution.o | tail -190 | head -40 > hw2_mystery.solution.lst diff -y hw2_mystery.solution.lst hw2c.lst
- The output is shown below. When only 1 vertical bar separates the two columns, then the program on the left matches the program on the right. When double vertical bars appear, there is a difference between the two programs. Visual inspection will indicate where the problem bytes are...
000001f0 04 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 | 000001f0 04 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 | 00000200 20 6d 61 6b 65 73 20 70 65 72 66 65 63 74 2e 2e | 00000200 20 6d 61 6b 65 73 20 70 65 72 66 65 63 74 2e 2e | 00000210 2e 70 72 61 63 74 69 63 65 62 75 74 20 6e 6f 62 | 00000210 2e 70 72 61 63 74 69 63 65 62 75 74 20 6e 6f 62 | 00000220 6f 64 79 27 73 20 73 6f 20 77 68 79 20 3f 0a 0a | 00000220 6f 64 79 27 73 20 73 6f 20 77 68 79 20 3f 0a 0a | 00000230 b8 04 00 00 00 bb 01 00 00 00 b9 11 00 00 00 ba | 00000230 b8 04 00 00 00 bb 01 00 00 00 b9 11 00 00 00 ba | 00000240 08 00 00 00 cd 80 b8 ff ff ff ff b8 04 00 00 00 | | 00000240 08 00 00 00 cd 80 b8 04 00 00 00 bb 01 00 00 00 | 00000250 bb 01 00 00 00 b9 07 00 00 00 b9 00 00 00 00 ba | | 00000250 b9 07 00 00 00 b9 00 00 00 00 ba 10 00 00 00 cd | 00000260 07 00 00 00 cd 80 ba 11 00 00 00 b8 01 00 00 00 | | 00000260 80 ba 11 00 00 00 b8 01 00 00 00 b8 04 00 00 00 | 00000270 b8 04 00 00 00 bb 01 00 00 00 b9 07 00 00 00 ba | | 00000270 bb 01 00 00 00 b9 07 00 00 00 ba 0a 00 00 00 cd | 00000280 0a 00 00 00 cd 80 b8 04 00 00 00 bb 01 00 00 00 | | 00000280 80 b8 04 00 00 00 bb 01 00 00 00 b9 19 00 00 00 | 00000290 b9 19 00 00 00 ba 0d 00 00 00 cd 80 b8 04 00 00 | | 00000290 ba 0d 00 00 00 cd 80 b8 04 00 00 00 bb 01 00 00 | 000002a0 00 bb 01 00 00 00 b9 06 00 00 00 ba 0a 00 00 00 | | 000002a0 00 b9 06 00 00 00 ba 0a 00 00 00 41 cd 80 b8 04 | 000002b0 41 cd 80 b8 04 00 00 00 bb 01 00 00 00 b9 24 00 | | 000002b0 00 00 00 bb 01 00 00 00 b9 24 00 00 00 41 ba 07 | 000002c0 00 00 41 ba 07 00 00 00 41 cd 80 b8 04 00 00 00 | | 000002c0 00 00 00 41 cd 80 b8 04 00 00 00 bb 01 00 00 00 | 000002d0 bb 01 00 00 00 b9 11 00 00 00 ba 08 00 00 00 cd | | 000002d0 b9 0e 00 00 00 ba 0b 00 00 00 cd 80 b8 04 00 00 | 000002e0 80 b8 04 00 00 00 bb 01 00 00 00 b9 2d 00 00 00 | | 000002e0 00 bb 01 00 00 00 b9 2d 00 00 00 ba 03 00 00 00 | 000002f0 ba 03 00 00 00 cd 80 b8 01 00 00 00 bb 00 00 00 | | 000002f0 cd 80 b8 01 00 00 00 bb 00 00 00 00 cd 80 00 00 | 00000300 00 cd 80 00 00 00 00 00 00 00 00 00 00 00 00 00 | | 00000300 00 54 68 65 20 4e 65 74 77 69 64 65 20 41 73 73 |