You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Visual Studio中MASM x64汇编(Intel语法)简易IO完整指南

Minimal x64 MASM (Intel Syntax) Input/Output Example for Visual Studio

Got it, let's keep this straightforward—here's a minimal, working example that handles two 32-bit integer inputs, runs a calculation, and outputs the result using MASM with Intel syntax in Visual Studio. I'll also cover setup steps and break down the code so you understand every part.

Full Code

externdef GetStdHandle: proc
externdef ReadConsoleA: proc
externdef WriteConsoleA: proc
externdef ExitProcess: proc

.data
    prompt1 db "Enter first 32-bit integer: ", 0
    prompt2 db "Enter second 32-bit integer: ", 0
    resultMsg db "Sum of the two integers: ", 0
    buffer db 16 dup(0)  ; Buffer to hold input strings
    num1 dd 0            ; Store first 32-bit integer
    num2 dd 0            ; Store second 32-bit integer
    sum dd 0             ; Store calculation result
    bytesRead dd 0       ; Track bytes read from input
    bytesWritten dd 0    ; Track bytes written to output

.code
main proc
    ; Get standard input handle and save it
    mov rcx, -10         ; Constant for STD_INPUT_HANDLE
    call GetStdHandle
    mov rbx, rax         ; Keep input handle in rbx for reuse

    ; Print first prompt
    mov rcx, -11         ; Constant for STD_OUTPUT_HANDLE
    call GetStdHandle
    mov rcx, rax         ; First arg: output handle
    lea rdx, prompt1     ; Second arg: pointer to prompt string
    mov r8, 28           ; Third arg: length of prompt1
    lea r9, bytesWritten ; Fourth arg: pointer to track bytes written
    mov qword ptr [rsp+32], 0 ; Reserved fifth arg (must be 0)
    call WriteConsoleA

    ; Read first input string
    mov rcx, rbx         ; First arg: input handle
    lea rdx, buffer      ; Second arg: input buffer
    mov r8, 16           ; Third arg: max chars to read
    lea r9, bytesRead    ; Fourth arg: pointer to track bytes read
    mov qword ptr [rsp+32], 0 ; Reserved fifth arg
    call ReadConsoleA

    ; Convert input string to 32-bit integer (store in num1)
    lea rsi, buffer
    call str_to_int
    mov num1, eax

    ; Print second prompt
    mov rcx, -11
    call GetStdHandle
    mov rcx, rax
    lea rdx, prompt2
    mov r8, 29           ; Length of prompt2
    lea r9, bytesWritten
    mov qword ptr [rsp+32], 0
    call WriteConsoleA

    ; Read second input string
    mov rcx, rbx
    lea rdx, buffer
    mov r8, 16
    lea r9, bytesRead
    mov qword ptr [rsp+32], 0
    call ReadConsoleA

    ; Convert input string to 32-bit integer (store in num2)
    lea rsi, buffer
    call str_to_int
    mov num2, eax

    ; Perform addition (swap this for other operations as needed)
    mov eax, num1
    add eax, num2
    mov sum, eax

    ; Print result prompt
    mov rcx, -11
    call GetStdHandle
    mov rcx, rax
    lea rdx, resultMsg
    mov r8, 27           ; Length of resultMsg
    lea r9, bytesWritten
    mov qword ptr [rsp+32], 0
    call WriteConsoleA

    ; Convert sum to string and print it
    mov eax, sum
    lea rdi, buffer
    call int_to_str
    ; Calculate length of the result string
    mov rcx, rdi
    sub rcx, buffer
    ; Write the result string to console
    mov rdx, buffer
    mov r8, rcx
    lea r9, bytesWritten
    mov qword ptr [rsp+32], 0
    call WriteConsoleA

    ; Exit the program cleanly
    mov rcx, 0
    call ExitProcess
main endp

; Helper: Convert null-terminated string to 32-bit integer (returns result in eax)
; Input: rsi = pointer to input string
str_to_int proc
    xor eax, eax         ; Initialize result to 0
    xor ecx, ecx         ; Clear ecx for digit storage
next_char:
    mov cl, [rsi]
    cmp cl, 0Dh          ; Stop at carriage return (Enter key)
    je done
    cmp cl, 30h          ; Skip chars below '0'
    jb done
    cmp cl, 39h          ; Skip chars above '9'
    ja done
    sub cl, 30h          ; Convert ASCII digit to integer
    imul eax, 10         ; Multiply current result by 10
    add eax, ecx         ; Add new digit to result
    inc rsi              ; Move to next character
    jmp next_char
done:
    ret
str_to_int endp

; Helper: Convert 32-bit integer to string (returns rdi pointing to end of string)
; Input: eax = integer to convert
int_to_str proc
    mov ebx, 10          ; Divisor for digit extraction
    mov ecx, 0           ; Count of digits
    ; Handle negative numbers (optional, but useful for negative inputs)
    test eax, eax
    jns push_digits
    neg eax
    mov byte ptr [rdi], '-'
    inc rdi
push_digits:
    xor edx, edx         ; Clear edx for division
    div ebx              ; Divide eax by 10, remainder in edx
    add dl, 30h          ; Convert remainder to ASCII digit
    push rdx             ; Push digit to stack
    inc ecx              ; Increment digit count
    test eax, eax        ; Check if there are more digits
    jnz push_digits
pop_digits:
    pop rdx              ; Pop digit from stack
    mov [rdi], dl        ; Write digit to buffer
    inc rdi              ; Move to next buffer position
    dec ecx              ; Decrement digit count
    jnz pop_digits
    mov byte ptr [rdi], 0Ah ; Add newline at the end
    inc rdi
    ret
int_to_str endp

end

Setup in Visual Studio

  • Create a new Empty Project with the x64 platform:
    1. Open Visual Studio → Create a new project → Search for "Empty Project" → Select it.
    2. Name your project, then in the "Configure your new project" window, set the "Platform" dropdown to x64.
  • Add an assembly file:
    1. Right-click your project in Solution Explorer → Add → New Item.
    2. Select "Text File", name it main.asm, and click Add.
  • Configure the project for MASM:
    1. Right-click the project → Build Dependencies → Build Customizations.
    2. Check the box for masm(.targets,.props) → Click OK.
    3. Right-click main.asm → Properties → Set "Item Type" to Microsoft Macro Assembler → Click OK.

Key Code Explanations

  • External Windows APIs: We use standard Windows console functions for I/O since x64 MASM in Visual Studio relies on the Windows runtime. GetStdHandle gets handles for input/output, ReadConsoleA reads user input, WriteConsoleA writes to the console, and ExitProcess closes the program.
  • Data Section: Stores prompts, an input buffer, and variables to hold our integers and track I/O operations.
  • Main Procedure:
    1. Fetches and saves input/output handles for reuse.
    2. Prints prompts to guide the user.
    3. Reads input strings, then converts them to 32-bit integers using the str_to_int helper.
    4. Runs a simple addition (replace this with your desired operation—see notes below).
    5. Converts the result back to a string with int_to_str and prints it.
  • Helper Functions:
    • str_to_int: Converts the input string (from ReadConsoleA) to a 32-bit integer, stopping when it hits the carriage return (sent when you press Enter).
    • int_to_str: Converts a 32-bit integer back to a string, including support for negative numbers.

Customization Tips

  • Change the Calculation: Replace the addition code with other operations:
    • Subtraction: mov eax, num1 → sub eax, num2
    • Multiplication: mov eax, num1 → imul eax, num2 (note: this produces a 64-bit result in edx:eax, so adjust storage if you need the full result)
    • Division: mov eax, num1 → cdq → idiv num2 (quotient in eax, remainder in edx)
  • Add Error Checking: For production code, you'd check the return values of API calls (e.g., ensure GetStdHandle doesn't return invalid handles) and handle invalid input (non-digit characters).

内容的提问来源于stack exchange,提问作者user9707187

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:03:17