0

My teacher assigned us this worksheet and it is really giving me trouble. I'm having trouble knowing exactly what I'm supposed to multiply or divide the index instruction from and then determining overflow based on that.

For instance on letter E we have

imulw 12(%ebx, %edx, 8)

and the registers that change are dx:ax since the operand size is 16 bits. The answer comes out to be

0004:CA63

but I'm having trouble understanding where the 0004 came from.

worksheet teacher gave us

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 1
    Two 16-bit values multiplied give a 32-bit product. The m.s. part is placed in `dx` and the l.s. part in `ax`. – Weather Vane Apr 07 '19 at 00:32
  • i understand that part. but when i multiply 0xDBAD * 0x4F im getting 43 CA63 – littlemimus Apr 07 '19 at 00:47
  • The value at memory location `0x51C` is `0xFADEDBAD`. The least 16 bits of this (little-endian) are multiplied by `ax` which is `0x004F` and the product is `0043CA63` held in `dx` and `ax`. – Weather Vane Apr 07 '19 at 00:49
  • 1
    The work sheet shows the answer for `dx` as `0043:` not `0004:`. – Weather Vane Apr 07 '19 at 00:54
  • yea i figured out the first one. I did the same steps i did on that one for letter E and i was getting the answer as 43:CA63 when someone from an earlier class had their worksheet graded and the answer for E was 0004:CA63 – littlemimus Apr 07 '19 at 00:56
  • At (E) I make it `FFF4CA63` because it is a signed multiplication. `0xDBAD * 0x004F` is decimal `-9299 * 79 = -734621 = 0xFFF4CA63`. – Weather Vane Apr 07 '19 at 01:06
  • ahhh that totally makes sense. so anytime its signed i have to set the leading bits to f – littlemimus Apr 07 '19 at 01:14

1 Answers1

0

You've been asked what the computer will do. So ask the computer what it will do (https://youtu.be/xaVgRj2e5_s?t=167):

# Linux:
# Name:     computer.s (capital S if compiled in Windows)
# Compile:  gcc -m32 -o computer computer.s
# Run:      ./computer  (Don't forget dot-slash!)

#if defined(__WIN32__)
    #define main _main
    #define printf _printf
#endif

.data
    arr:    .long 0x5, 0x7, 0x8, 0xD, 0x9, 0xC1A55ED, 0xDECADE, 0xFADEDBAD, 0x700300, 0x400800
    fmt:   .string "%c % 4X % 8X:%- 8X OF=%c\n"

.text
.global main
main:
    pushl %ebp
    movl %esp, %ebp
    subl $24, %esp              # Space for 6 DWORD à 4 byte
    andl $-16, %esp             # Align stack to 16

E:  call init
    lea 12(%ebx,%edx,8), %edi   # Claculate the operand and store it in EDI

    movl $arr, %ebx             # Modify EBX to point to arr
    imulw 12(%ebx, %edx, 8)     # Do it
    seto %cl                    # Store the overflow flag in CL
    or $0x30, %cl               # To ASCII
    movzwl %ax, %eax            # Clear garbage in EAX
    movzwl %dx, %edx            # Clear garbage in EDX

    movl $fmt, 0(%esp)          # Fill the stack with arguments for printf
    movl $'E', 4(%esp)
    movl %edi, 8(%esp)
    movl %edx, 12(%esp)
    movl %eax, 16(%esp)
    movl %ecx, 20(%esp)
    call printf                 # Call a C function

    leave                       # Restore the stack
    xor %eax, %eax              # Return 0;
    ret                         # Only valid when compiled with a compiler (GCC)

init:
    movl $0x4F, %eax
    movl $0x500, %ebx
    movl $0x1, %ecx
    movl $0x2, %edx
    ret
rkhb
  • 14,159
  • 7
  • 32
  • 60