首页 > 解决方案 > 整数乘法和索引寻址

问题描述

我的老师给我们分配了这张工作表,这真的给我带来了麻烦。我很难确切地知道我应该从什么乘或除索引指令,然后根据它确定溢出。

例如在字母 E 上,我们有

imulw 12(%ebx, %edx, 8)

并且更改的寄存器是 dx:ax 因为操作数大小是 16 位。答案是

0004:CA63

但我无法理解 0004 的来源。

工作表老师给了我们

标签: assemblyx86multiplicationsignedaddressing-mode

解决方案


你被问到电脑会做什么。所以问问电脑它会做什么(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

推荐阅读