如何确定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:
-128to127. Only use for numbers within this narrow range. - 2 bytes: Signed 16-bit integer. Range:
-32768to32767. Your test case with32767works here (you may have used1by accident, but it likely functioned due to unintended macro behavior with larger values). - 4 bytes: Signed 32-bit integer. Range:
-2147483648to2147483647. This is the standard size for most 32-bit program use cases.
Your Specific Questions
- For the number 6518846: This value (~6.5 million) falls well within the 32-bit signed integer range. Use
PRINT_DEC 4, 6518846to avoid overflow and ensure correct positive output. - Why 32768 outputs negative: When using
PRINT_DEC 2, 32768,32768equals0x8000in hex—the exact value representing-32768in signed 16-bit integers. To print32768as a positive number, either:- Use
PRINT_UNS 2, 32768(treats the value as an unsigned 16-bit integer), or - Use
PRINT_DEC 4, 32768(since32768is within the 32-bit signed positive range).
- Use
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
相关产品推荐
相关产品推荐

