组装-以零填充移动以获取不同的数据大小 [英] Assembly - Move with Zero Fill for Different Data Sizes

查看:59
本文介绍了组装-以零填充移动以获取不同的数据大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在使用Masm学习汇编语言.这是我班上的作业.

Currently learning assembly language using Masm. This is for an assignment in my class.

我必须使用32位寄存器(EAX和EBX)进行某些计算.我必须处理BYTE,WORD和DWORD变量.并不复杂.我不太明白为什么在组装当前代码时会出现这么多错误:

I must do certain calculations using 32-bit registers (EAX and EBX). I have to handle BYTE, WORD, and DWORD variables. Not really complicated. I do not really understand why am I getting so many errors when assembling the current code:

INCLUDE Irvine32.inc

.data
    ; (declare variables)
    bNum01      BYTE    64
    bNum02      BYTE    32
    bNum03      BYTE    16
    bSum        BYTE    ?
    bDiff       BYTE    ?
    bResult     BYTE    ?

    wNum01      WORD    64
    wNum02      WORD    32
    wNum03      WORD    16
    wSum        WORD    ?
    wDiff       WORD    ?
    wResult     WORD    ?

    dwNum01     DWORD   64
    dwNum02     DWORD   32
    dwNum03     DWORD   16
    dwSum       DWORD   ?
    dwDiff      DWORD   ?
    dwResult    DWORD   ?
    dwTotal     DWORD   ?


.code
main PROC
    ; (insert executable instructions)
    movzx   eax,bNum01
    add eax,bNum02
    add eax,bNum03
    movzx   bSum,eax

    movzx   eax,bNum02
    add eax,bNum03
    sub eax,bNum01
    movzx   bDiff,eax

    movzx   eax,bSum
    add eax,bDiff
    movzx   bResult,eax

    mov  esi,OFFSET   bSum
    mov  ecx,LENGTHOF bSum
    mov  ebx,TYPE     bSum
    call DumpMem             ; call Dump Memory for selected offset value

    mov  esi,OFFSET   bDiff
    mov  ecx,LENGTHOF bDiff
    mov  ebx,TYPE     bDiff
    call DumpMem             ; call Dump Memory for selected offset value   

    mov  esi,OFFSET   bResult
    mov  ecx,LENGTHOF bResult
    mov  ebx,TYPE     bResult
    call DumpMem             ; call Dump Memory for selected offset value


; WORD main processing


    movzx   ebx,wNum01
    add ebx,wNum02
    add ebx,wNum03
    movzx   wSum,ebx

    movzx   ebx,wNum02
    add ebx,wNum03
    sub ebx,wNum01
    movzx   wDiff,ebx

    movzx   ebx,wSum
    add ebx,wDiff
    movzx   wResult,ebx

    mov  esi,OFFSET   wSum
    mov  ecx,LENGTHOF wSum
    mov  ebx,TYPE     wSum
    call DumpMem             ; call Dump Memory for selected offset value

    mov  esi,OFFSET   wDiff
    mov  ecx,LENGTHOF wDiff
    mov  ebx,TYPE     wDiff
    call DumpMem             ; call Dump Memory for selected offset value   

    mov  esi,OFFSET   wResult
    mov  ecx,LENGTHOF wResult
    mov  ebx,TYPE     wResult
    call DumpMem             ; call Dump Memory for selected offset value


; DWORD main processing

    mov     eax,0
    mov eax,dwNum01
    add eax,dwNum02
    add eax,dwNum03
    mov dwSum,eax

    mov eax,0
    mov eax,dwNum02
    add eax,dwNum03
    sub eax,dwNum01
    mov dwDiff,eax

    mov eax,0
    mov eax,dwSum
    add eax,dwDiff
    mov dwResult,eax

    mov  esi,OFFSET   dwSum
    mov  ecx,LENGTHOF dwSum
    mov  ebx,TYPE     dwSum
    call DumpMem             ; call Dump Memory for selected offset value

    mov  esi,OFFSET   dwDiff
    mov  ecx,LENGTHOF dwDiff
    mov  ebx,TYPE     dwDiff
    call DumpMem             ; call Dump Memory for selected offset value   

    mov  esi,OFFSET   dwResult
    mov  ecx,LENGTHOF dwResult
    mov  ebx,TYPE     dwResult
    call DumpMem             ; call Dump Memory for selected offset value

; Main side for Dwtotal


    mov eax,0
    movzx eax,bSum

    add ebx,wSum
    add eax,dwSum

    mov dwTotal,eax


    mov  esi,OFFSET   dwTotal
    mov  ecx,LENGTHOF dwTotal
    mov  ebx,TYPE     dwTotal
    call DumpMem             ; call Dump Memory for selected offset value


    exit
main ENDP

    ; (additional procedures)
END main

错误开始于使用MOVZX将Bnum01的值移至EAX寄存器后,将bNum02添加至EAX寄存器的行.它只是说这是一个无效的指令操作数".据我了解,它告诉我那不是它的工作原理".我尝试进行研究,但没有任何真正意义.

The errors start in the line where I add bNum02 to the EAX register after using MOVZX to move the value of Bnum01 to the EAX register. It just says that it is an "invalid instruction operands". Which, from what I understand, it is telling me "That's not how it works". I try to researched but nothing really made sense.

感谢所有花时间解释的人.

Thank you to anyone that takes the time to explain.

推荐答案

指令的操作数必须具有相同的类型.尺寸.您不能将 BYTE 变量移至 DWORD 寄存器.用于此目的的特殊指令是 MOVZX MOVSX ,它们分别转换较小的操作数.无法以这种方式存储( movzx bSum,eax ),但是您可以使用 EAX 的最低有效字节- AL 寄存器:

The operands of an instruction must have the same type resp. size. You cannot move a BYTE variable to a DWORD register. The special instructions for that purpose are MOVZX and MOVSX which convert the smaller operand appropriate. Storing in this way (movzx bSum,eax ) is not possible, but you can use the least significant byte of EAX - the AL register:

movzx   eax,bNum01
movzx   ebx,bNum02
add eax, ebx
movzx   ebx,bNum03
add eax, ebx
mov   bSum, al

EBX 的最低有效字称为 BX 寄存器(EAX-> AX,EBX-> BX,ECX-> CX,EDX-> DX):

The least significant WORD of EBX is called BX register (EAX->AX, EBX->BX, ECX->CX, EDX->DX):

mov   wSum, bx

这篇关于组装-以零填充移动以获取不同的数据大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆