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

如何确定SASM(NASM)32位程序中PRINT_DEC宏的所需字节数?

How to Choose Byte Count for SASM's PRINT_DEC Macro

The first parameter to PRINT_DEC specifies the size (in bytes) of the signed integer you want to print. Each size maps to a strict valid value range—using a size too small for your number triggers sign-extension overflow, resulting in negative output.

Valid Size Ranges for PRINT_DEC

  • 1 byte: Signed 8-bit integer. Range: -128 to 127. Only use for numbers within this narrow range.
  • 2 bytes: Signed 16-bit integer. Range: -32768 to 32767. Your test case with 32767 works here (you may have used 1 by accident, but it likely functioned due to unintended macro behavior with larger values).
  • 4 bytes: Signed 32-bit integer. Range: -2147483648 to 2147483647. This is the standard size for most 32-bit program use cases.

Your Specific Questions

  1. For the number 6518846: This value (~6.5 million) falls well within the 32-bit signed integer range. Use PRINT_DEC 4, 6518846 to avoid overflow and ensure correct positive output.
  2. Why 32768 outputs negative: When using PRINT_DEC 2, 32768, 32768 equals 0x8000 in hex—the exact value representing -32768 in signed 16-bit integers. To print 32768 as a positive number, either:
    • Use PRINT_UNS 2, 32768 (treats the value as an unsigned 16-bit integer), or
    • Use PRINT_DEC 4, 32768 (since 32768 is within the 32-bit signed positive range).

Fixed Example Code

To print values correctly and safely:

%include "io.inc"

section .text
global CMAIN
CMAIN:
    PRINT_DEC 2, 32767   ; Correct size for 16-bit signed maximum
    PRINT_DEC 4, 32768   ; Prints 32768 as positive (32-bit signed)
    PRINT_UNS 2, 32768   ; Also prints 32768 as positive (16-bit unsigned)

    xor eax, eax
    ret

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 07:57:03