整数乘法和索引寻址

问题描述 投票:0回答:1

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

例如,我们有字母E.

imulw 12(%ebx, %edx, 8)

由于操作数大小为16位,因此更改的寄存器为dx:ax。答案出来了

0004:CA63

但是我无法理解0004来自哪里。

assembly x86 multiplication signed addressing-mode
1个回答
0
投票

你被问到计算机会做什么。所以问电脑会做什么(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
© www.soinside.com 2019 - 2024. All rights reserved.