我在Visual Studio上使用的是带有Irvine 32库的MASM语法。
我试图使这个程序转换二进制到十进制,反之亦然,现在我坚持对如何转换二进制到十六进制,反之亦然
这是我的代码
INCLUDE Irvine32.inc
.data
str1 BYTE ">>> Please select the conversion type: ", 0
str2 BYTE " 1. Binary to Decimal", 0
str3 BYTE " 2. Decimal to Binary", 0
str4 BYTE " 3. Exit", 0
str5 BYTE "-----------------------------------------", 0
str6 BYTE "Enter your choice: ", 0
str7 BYTE "Please Enter 8-bit binary digits (e.g., 11110000): ", 0
str8 BYTE "The decimel integer of ", 0
str9 BYTE " is ", 0
str10 BYTE " ", 0
str11 BYTE "Please Enter a decimal integer less than 256: ", 0
str12 BYTE "The binary of ", 0
str13 BYTE " is ", 0
str14 BYTE " ", 0
str15 BYTE "Bye.", 0
choice DWORD ?
num1 BYTE 9 DUP(? )
num2 WORD ?
numLength DWORD 0
outLoop DWORD 0
base DWORD 2
deciNum DWORD ?
.code
main PROC
_mainprog : ;USED TO DISPLAY THE MENU & EXIT
mov edx, offset str1
call WriteString
call crlf
mov edx, offset str2
call WriteString
call crlf
mov edx, offset str3
call WriteString
call crlf
mov edx, offset str4
call WriteString
call crlf
mov edx, offset str5
call WriteString
call crlf
mov edx, offset str6
call WriteString
call ReadDec ;DISPLAY OUTPUT
mov choice, eax ;EAX = 1
cmp choice, 1 ;IF = 1
je _proc1 ;JUMP TO PROC1
cmp choice, 2 ;IF = 2
je _proc2 ;JUMP TO PROC2
cmp choice, 3 ;IF = 3
je _proc3 ;JUMP TO PROC3
_proc1 : ;BINARY TO DECIMAL
mov edx, offset str7
call WriteString
mov edx, offset num1 ;ASSIGN NUM1 TO EDX TO WRITE THE BINARY IN STRING FORMAT
mov ecx, sizeof num1 ;ASSIGN TO ECX TO ACT AS THE COUNTER (COUNT THE LENGTH OF THE BINARY DIGIT)
call ReadString ;USER INPUT BINARY NUMBER
mov numLength, eax ;USER INPUT EAX STORED IN NUMLENGTH
mov eax, 0 ;EAX ASSIGNED TO 8
mov esi, 0 ;ESI TO ACCESS THE STRING BINARY INPUT THAT WE ENTERED
mov ecx, numLength
whileProc1 :
cmp ecx, 0 ;COMPARE COUNTER WITH 0
je printResult ;IF ECX =/ 0, IT WILL NOT JUMP TO PRINT RESULT
mov outLoop, ecx ;8 ASSIGNED TO OUTLOOP AND ASSIGNED TO REGISTER ECX
ifProc :
cmp num1[esi], '0' ;IF 1ST VALUE IN THE BINARY DIGIT = 0
je incEsiProc ;JUMP TO INCREASE ESI PROC
elseIfProc:
cmp num1[esi], '1' ;IF 1ST VALUE IN BINARY DIGIT = 1
mov ecx, numLength ;NUMLENGTH ASSIGNED TO ECX
sub ecx, esi ;
dec ecx
mov eax, 1
whileProc2 :
je stopProc
mov ebx, base
mul ebx
dec ecx
jmp whileProc2
stopProc :
add deciNum, eax
jmp incEsiProc
incEsiProc :
inc esi
mov ecx, outLoop
dec ecx
jmp whileProc1
printResult :
mov edx, offset str8
call WriteString
mov edx, offset num1
call WriteString
mov edx, offset str9
call WriteString
mov eax, deciNum
call WriteDec
mov edx, offset str10
call WriteString
call crlf
call crlf
jmp _mainprog
_proc2: ;DECIMAL TO BINARY
mov edx, offset str11
call WriteString
mov edx, offset num2
call ReadInt
mov num2, ax
mov ebx, 1
mov edx, offset str12
call WriteString
mov ax, num2
call WriteDec
mov edx, offset str13
call WriteString
call WriteBinB
mov edx, offset str14
call WriteString
call crlf
call crlf
jmp _mainprog
_proc3:
mov edx, offset str15
call WriteString
call crlf
call WaitMsg
exit
main ENDP
END main
能够要求用户输入一个数字和转换十六进制到二进制/十进制,反之亦然
1条答案
按热度按时间oewdyzsn1#
这就是程序现在的样子,我用这个算法将二进制字符串转换为整数。:)
1.二进制到十进制
1.十进制到二进制
1.二进制到十六进制
1.十六进制到二进制