Difference between revisions of "CSC231 Homwork 2 Solution 2010"

From dftwiki3
Jump to: navigation, search
(Mystery Program)
(Mystery Program)
Line 3: Line 3:
  
 
__TOC__
 
__TOC__
 +
 +
=Logic Design=
 +
 +
* To test the validity of boolean function, a simple Python program can be of great help.
 +
 +
<code><pre>
 +
#! /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()
 +
 +
</pre></code>
 +
  
 
=Mystery Program=
 
=Mystery Program=

Revision as of 09:45, 7 October 2010

--D. Thiebaut 21:35, 6 October 2010 (UTC)


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