Difference between revisions of "CSC231 Homework 7 2015"

From dftwiki3
Jump to: navigation, search
 
(18 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
<showafterdate after="20151111 12:00" before="20151231 00:00">
 
<showafterdate after="20151111 12:00" before="20151231 00:00">
 
<bluebox>
 
<bluebox>
 +
This assignment is due on Wednesday, <strike>11/18/15</strike> 11/25/15, at 11:55 p.m.  Document your program well!
 
</bluebox>
 
</bluebox>
=Problem 1=
 
 
<br />
 
<br />
 +
=Problem=
 
<br />
 
<br />
Your assignment is to add a new function to '''231Lib2.asm''' called '''_printRegs'''.
 
 
<br />
 
<br />
* Take your 231Lib.asm library and make a copy of it called '''231Lib2.asm'''.
+
Your assignment is to add a new function to '''231Lib.asm''' called '''_printRegs'''.
 +
<br />
 +
* Take your 231Lib.asm library and make a copy of it, and call it '''231Lib2.asm'''.
 
   
 
   
 
   cp 231Lib.asm 231Lib2.asm
 
   cp 231Lib.asm 231Lib2.asm
 
   
 
   
* Add a '''global'''statement at the top of the program (with the other similar statements):
+
* Add a '''global''' statement at the top of the program (with the other similar statements):
 
<br />
 
<br />
 
::<source lang="asm">
 
::<source lang="asm">
Line 61: Line 63:
 
                 mov  eax, 0xFFFFFFFF
 
                 mov  eax, 0xFFFFFFFF
 
                 mov  ebx, 0
 
                 mov  ebx, 0
                 mov  ecx, 0x80000000
+
                 mov  ecx, 0x00000003
 
                 mov  edx, 0x7FFFFFFF
 
                 mov  edx, 0x7FFFFFFF
 
                 mov  esi,  2
 
                 mov  esi,  2
 
                 mov  edi, 0x80000002
 
                 mov  edi, 0x80000002
 
   
 
   
 +
                call    _printRegs
 +
                call    _println
 
                 call    _printRegs
 
                 call    _printRegs
 
                 call    _println
 
                 call    _println
Line 78: Line 82:
 
                 call    _printRegs
 
                 call    _printRegs
 
                 call    _println
 
                 call    _println
 +
 +
;;; exit
 +
mov ebx, 0
 +
mov eax, 1
 +
int 0x80
  
 
</source>
 
</source>
Line 86: Line 95:
 
  eax FFFFFFFF 4294967295 -1
 
  eax FFFFFFFF 4294967295 -1
 
  ebx 00000000 0 0
 
  ebx 00000000 0 0
  ecx 80000000 2147483648 -2147483648
+
  ecx 00000003 3 3
  edx 7FFFFFFF 2147483647 -2147483649
+
edx 7FFFFFFF 2147483647 2147483647
 +
edi 80000002 2147483650 -2147483646
 +
esi 00000002 2 2
 +
 +
eax FFFFFFFF 4294967295 -1
 +
ebx 00000000 0 0
 +
ecx 00000003 3 3
 +
  edx 7FFFFFFF 2147483647 2147483647
 
  edi 80000002 2147483650 -2147483646
 
  edi 80000002 2147483650 -2147483646
 
  esi 00000002 2 2
 
  esi 00000002 2 2
Line 97: Line 113:
 
  edi FFFFFFFE 4294967294 -2
 
  edi FFFFFFFE 4294967294 -2
 
  esi 0000000A 10 10
 
  esi 0000000A 10 10
 +
 +
<br />
 +
==Hints==
 +
<br />
 +
* Using temporary variables is fine for this assignment.
 +
* If you write functions, make them "clean," in the sense that they should return to their caller without changing any of the registers.  This will make your life much easier!  For example, you may want to create a '''printSpace''' function.  Here's how I would code it:
 +
<br />
 +
::<source lang="asm">
 +
;;;-----------------------------------------------------------------
 +
;;; printSpace: prints a space.  Does not modify any of the
 +
;;; registers.
 +
;;;-----------------------------------------------------------------
 +
                section  .data
 +
space          db    ' '
 +
 +
                section  .text
 +
printSpace:       
 +
                push    ecx
 +
                push    edx
 +
                mov    ecx, space
 +
                mov    edx, 1
 +
                call    _printString
 +
                pop    edx
 +
                pop    ecx
 +
                ret
 +
 +
</source>
 +
<br />
 +
* You may want to create a table of the 16  4-bit binary numbers I draw in class regularly, and available [[CSC231_4-bit_Unsigned_Nybble| here]].  Assume that the most significant bit of the group of 4 bits is a sign bit, and enter the values of the numbers in a 4th column.  Figure out the algorithm you have to use to actually print the numbers in the 4th column.  For example, how do you print 0010 as 2, but 1111 as -1?
 +
<br />
 +
==_PrintHex==
 +
<br />
 +
* You should also add the '''_printHex''' function to your 231Lib2.asm library.  Make sure you make it global, so that the linker will allow other assembly program linked with the library to call _printHex, if needed.
 +
<br />
 +
==Submission==
 +
<br />
 +
* Submit your 231Lib2.asm file on Moodle.  It will be tested with a different main program, that will set the registers to different values and verify that your '''_printRegs''' function prints them correctly.
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
</showafterdate>
 +
 +
<onlydft>
 +
::<source lang="python">
 +
text = """
 +
mov  eax, 0xFFFFFFFF
 +
                mov  ebx, 0
 +
                mov  ecx, 0x80000000
 +
                mov  edx, 0x7FFFFFFF
 +
                mov  esi,  2
 +
                mov  edi, 0x80000002
 
   
 
   
</showafterdate>
+
call
 +
                mov  eax, 0x01234567
 +
                mov  ebx, 0x000000FF
 +
                mov  ecx, 0x00000001
 +
                mov  edx, 0x0
 +
                mov  esi,  10
 +
                mov  edi, 0xFFFFFFFE
 +
 
 +
call
 +
                """
 +
 
 +
def printRegs( list1, list2 ):
 +
    print( list1 )
 +
    print( list2 )
 +
    for i in range( len( list1 ) ):
 +
        x = list2[i]
 +
        if x >= 0x8000000:
 +
            x = x - 2**32
 +
        print( list1[i], format( list2[i], '08X' ), list2[i], x )
 +
             
 +
list1 = ["eax", "ebx", "ecx", "edx", "edi", "esi" ]
 +
list2 = [ 0, 0, 0, 0, 0, 0 ]
 +
 
 +
for line in text.split("\n" ):
 +
    line = line.strip()
 +
    if line.find( "call" )!= -1:
 +
        printRegs( list1, list2 )
 +
        continue
 +
    for i in range( len( list1 ) ):
 +
        if line.find( list1[i] ) != -1:
 +
            list2[i] = eval( line.split( "," )[-1].strip() )
 +
 
 +
           
 +
           
 +
       
 +
</source>
 +
</onlydft>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC231]][[Category:Nasm]][[Category:Homework]]

Latest revision as of 08:48, 18 November 2015

--D. Thiebaut (talk) 11:58, 10 November 2015 (EST)


<showafterdate after="20151111 12:00" before="20151231 00:00">

This assignment is due on Wednesday, 11/18/15 11/25/15, at 11:55 p.m. Document your program well!


Problem



Your assignment is to add a new function to 231Lib.asm called _printRegs.

  • Take your 231Lib.asm library and make a copy of it, and call it 231Lib2.asm.
 cp 231Lib.asm 231Lib2.asm

  • Add a global statement at the top of the program (with the other similar statements):


global _printRegs


  • Go to the end of the file and add a new function called _printRegs


  • Your new function should display the contents of eax, ebx, ecx, edx, edi, and esi, in this order:
  • in hexadecimal (you should have a function for that)
  • as an unsigned number (_printDec will do that for you)
  • as a 2's complement signed number (you have to figure this part out).


  • Submit you 231Lib2.asm program to Moodle when done


Testing


  • To test your new function, create a new program that will be linked with your modified library, and will display all the registers.
  • Example test program:


;;;  test.asm
;;;  assemble and run as follows:
;;;
;;;  nasm -f elf test.asm
;;;  nasm -f elf 231Lib2.asm
;;;  ld -melf_i386 test.o 231Lib2.o -o myTest
;;;  ./myTest
;;;

;;;  Extern functions that will be linked to this program
;;;  they are contained in 231Lib2.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
extern  _printRegs

;;; ------------------------------------------------------
;;; CODE SECTION
;;; ------------------------------------------------------        
                section .text
                global  _start
_start: 
                mov   eax, 0xFFFFFFFF
                mov   ebx, 0
                mov   ecx, 0x00000003
                mov   edx, 0x7FFFFFFF
                mov   esi,  2
                mov   edi, 0x80000002
 
                call    _printRegs
                call    _println
                call    _printRegs
                call    _println

                mov   eax, 0x01234567
                mov   ebx, 0x000000FF
                mov   ecx, 0x00000001
                mov   edx, 0x0
                mov   esi,  10
                mov   edi, 0xFFFFFFFE

                call    _printRegs
                call    _println

;;; exit
		mov	ebx, 0
		mov	eax, 1
		int	0x80


The output of the program above should be:

eax FFFFFFFF 4294967295 -1
ebx 00000000 0 0
ecx 00000003 3 3 
edx 7FFFFFFF 2147483647 2147483647
edi 80000002 2147483650 -2147483646
esi 00000002 2 2

eax FFFFFFFF 4294967295 -1
ebx 00000000 0 0
ecx 00000003 3 3 
edx 7FFFFFFF 2147483647 2147483647
edi 80000002 2147483650 -2147483646
esi 00000002 2 2

eax 01234567 19088743 19088743
ebx 000000FF 255 255
ecx 00000001 1 1
edx 00000000 0 0
edi FFFFFFFE 4294967294 -2
esi 0000000A 10 10


Hints


  • Using temporary variables is fine for this assignment.
  • If you write functions, make them "clean," in the sense that they should return to their caller without changing any of the registers. This will make your life much easier! For example, you may want to create a printSpace function. Here's how I would code it:


;;;-----------------------------------------------------------------
;;; printSpace: prints a space.  Does not modify any of the 
;;; registers.
;;;-----------------------------------------------------------------
                section  .data
space           db     ' '

                section  .text
printSpace:        
                push    ecx
                push    edx
                mov     ecx, space
                mov     edx, 1
                call    _printString
                pop     edx
                pop     ecx
                ret


  • You may want to create a table of the 16 4-bit binary numbers I draw in class regularly, and available here. Assume that the most significant bit of the group of 4 bits is a sign bit, and enter the values of the numbers in a 4th column. Figure out the algorithm you have to use to actually print the numbers in the 4th column. For example, how do you print 0010 as 2, but 1111 as -1?


_PrintHex


  • You should also add the _printHex function to your 231Lib2.asm library. Make sure you make it global, so that the linker will allow other assembly program linked with the library to call _printHex, if needed.


Submission


  • Submit your 231Lib2.asm file on Moodle. It will be tested with a different main program, that will set the registers to different values and verify that your _printRegs function prints them correctly.





</showafterdate>


...