Lab #6 (11/07/11)                       Judy Franklin 2011             Odds and Ends in Arithmetic

SMITH COLLEGE      CSC231  Lab #6

  • Download a copy of sumiproc2.asm.
    This file is also in the handout directory. To retrieve it type
    getcopy sumiproc2.asm
    
  • Take a look at it, the values mov-ed into the registers, and the location of the label "past"
  • assemble, and load the file.
  • Run the file in gdb and set a break at label past:
    gdb ./sumiproc2
    break *past
    run
    print $eax    negative value in decimal
    print/t $eax  negative value in binary
    
    Do you see a lot of ones? This is how a negative value is represented, in two's complement.
    We can convert this to its positive value.
    1. Write down eax's binary value in hex. You should have 8 Hex characters.
    2. Subtract this 8 character hex value from FFFFFFFF.
    3. Add 1.
    4. Double check that you have the same value that you obtained (with opposite sign) in the gdb instruction print $eax. If not, try again until you do.
    There are other ways to represent integers that are signed (we have until now worked with unsigned integers). There is sign-magnitude and one's complement. But two's complement is very useful because both positive and negative values can be represented in two's complement, and they can be added together, and the correct sign is obtained. The sign of the value is the left-most, or most significant bit.
    A nice tutorial on Two's complement by Thomas Finley at Cornell.
    Notice that the binary value in eax had a 1 in the left-most bit.
  • The add and sub instructions work on both sign and unsigned values.
  • But there are two different instructions, imul and idiv for signed numbers.
    1. Get a copy of sqr.asm
    2. Look the file over.
      Now, assemble, load and run it.
      -11 squared should be 121.
      Change the imul instruction to mul and see what happens.
      Check both eax and edx. You will find eax contains 121, but edx holds a large value...or a negative value if it is interpreted as a signed value.
      Using io.asm, and calling Decimal, edx was set to 0:
      [jfrankli@beowulf 231]$ !./
      ./sqrJudy
      eax in mul
      4294967285  (Two's complement rep. in base 10. See below).
      eax then edx after mul
      121
      4294967274  i.e. edx contains  -22, two's complement
      eax in imul
      4294967285
      eax then edx after imul
      121
      0
      
      2^32 = 4294967296
      2^32-1 = 4294967295  (all ones in 32 bits) = FFFFFFFFFFFFFFFFx0
      2^32-1 -11 =4294967284, Then +1 for two's complement = 4294967285
      
  • Multiplying Big numbers.
    1. Now get a copy of multiplyBig.asm
    2. Look at it, nasm it, load it.
    3. gdb it, setting a break at the label done:
            (gdb) break *done
    4. run the program and when it hits the break point,
      print the hex values of eax and edx, then print their decimal values.
      Also print the contents of a and b, the two values being multiplied.
    5. How do we know if the answer edx:eax is correct?
    6. Try multiplying the decimal values of a and b together yourself (via calculator).
    7. Now, we could just take the 11 hex characters in edx:eax and figure out the corresponding decimal value. However, we do have the decimal value of eax already.
    8. So, just add a value calculated from the edx hex value to the decimal value of eax.
    9. Do your answers match? Why or why not? Is the answer positive or negative? Should it be positive or negative?