我只是在短短的一个学期(比如只有六周)内学会了这一点。仍然对整个汇编语言的事情感到困惑,这段代码也使用 chatgpt 来纠正一些错误,这是 chatgpt 给我的最终版本。仍然有问题,我不知道问题出在哪里。我运行代码后命令提示符只是空白。
TITLE AssignmentP2 (Mult.asm)
; This program calculates the value using the following data
; Val1 = 340d
; Val2 = A3Ch
; Val3 = 45h
; FinalVal = -(Val1 – Val3) + 1 + (Val2 * 3) + 1 + 1 – 2 -1– (Val1 / 2)
INCLUDE Irvine32.inc
INCLUDE macros.inc ; Include macros for WriteDec and Crlf
.data
Val1 dword 340d
Val2 dword 0A3Ch
Val3 dword 45h
.code
main proc
mov eax, [Val1] ; Load Val1 into eax
sub eax, [Val3] ; Subtract Val3 from eax
neg eax ; Negate eax (2's complement)
inc eax ; Add 1 to eax
mov ebx, [Val2] ; Load Val2 into ebx
imul ebx, 3 ; Multiply ebx by 3
add eax, ebx ; Add ebx to eax
inc eax ; Add 1 to eax
inc eax ; Add 1 to eax
sub eax, 2 ; Subtract 2 from eax
dec eax ; Subtract 1 from eax
mov edx, eax ; Preserve the result in edx
mov ebx, [Val1] ; Load Val1 into ebx
mov ecx, 2 ; Divisor (2)
div ecx ; Divide eax by ecx (Val1 / 2)
mov ecx, eax ; Store the result (Val1 / 2) in ecx
mov eax, edx ; Restore the preserved result into eax
call WriteDec ; Call WriteDec to print the result
main endp
end main
我期望的十进制答案是 12417。
打印-12058
TITLE AssignmentP2 (Mult.asm)
; This program calculates the value using the following data
; Val1 = 340d
; Val2 = A3Ch
; Val3 = 45h
; FinalVal = -(Val1 – Val3) + 1 + (Val2 * 3) + 1 + 1 – 2 -1– (Val1 / 2)
include \masm32\include\Irvine32.inc
includelib \masm32\lib\Irvine32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data
Val1 dword 340dh ; 13325
Val2 dword 0A3Ch ; 2620
Val3 dword 45h ; 69
eax_reg db "eax ",0
ebx_reg db "ebx ",0
ecx_reg db "ecx ",0
edx_reg db "edx ",0
hex_reg db "hex: ",0
dec_reg db " dec: ",0
show_reg db 1
.code
main proc
mov eax, [Val1] ; Load Val1 into eax
mov [show_reg],1
call output
sub eax, [Val3] ; Subtract Val3 from eax
mov [show_reg],1
call output
neg eax ; Negate eax (2's complement)
mov [show_reg],1
call output
inc eax ; Add 1 to eax
mov [show_reg],1
call output
mov ebx, [Val2] ; Load Val2 into ebx
mov [show_reg],2
call output
imul ebx, 3 ; Multiply ebx by 3
mov [show_reg],2
call output
add eax, ebx
mov [show_reg],1
call output
inc eax ; Add 1 to eax
mov [show_reg],1
call output
inc eax ; Add 1 to eax
mov [show_reg],1
call output
sub eax, 2 ; Subtract 2 from eax
mov [show_reg],1
call output
dec eax ; Subtract 1 from eax
mov [show_reg],1
call output
mov ebx, [Val1]
mov [show_reg],2
call output
shr ebx, 1
mov [show_reg],2
call output
sub eax, ebx
mov [show_reg],1
call output
exit
ret
main endp
output proc
cmp [show_reg], 1
jz eax_reg_off
cmp [show_reg], 2
jz ebx_reg_off
cmp [show_reg], 3
jz ecx_reg_off
cmp [show_reg], 4
jz edx_reg_off
eax_reg_off:
mov edx, offset eax_reg
call WriteString
mov edx, offset hex_reg
call WriteString
call WriteHex
mov edx, offset dec_reg
call WriteString
call WriteInt
call crlf
jmp print_done
ebx_reg_off:
push eax
mov eax, ebx
mov edx, offset ebx_reg
call WriteString
mov edx, offset hex_reg
call WriteString
call WriteHex
mov edx, offset dec_reg
call WriteString
call WriteInt
call crlf
pop eax
jmp print_done
ecx_reg_off:
push eax
mov eax, ecx
mov edx, offset hex_reg
call WriteString
call WriteHex
mov edx, offset dec_reg
call WriteString
call WriteInt
call crlf
pop eax
jmp print_done
edx_reg_off:
push eax
mov eax, edx
mov edx, offset hex_reg
call WriteString
call WriteHex
mov edx, offset dec_reg
call WriteString
call WriteInt
call crlf
pop eax
jmp print_done
print_done:
ret
output endp
end main