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

HLA汇编程序调试求助:快餐订单金额计算功能异常

HLA快餐订单金额计算程序问题修复

问题核心原因

  1. 位段提取逻辑错误:未正确从16位输入值中拆分出对应各商品数量的字段,导致数量显示混乱。
  2. 总金额计算缺失:程序直接将输入的十六进制值作为总金额输出,完全跳过了“数量×单价”的累加计算,这就是总金额与输入值完全一致的根本原因。

修复后的完整代码

program FastFoodCalculator;
#include( "stdlib.hhf" )

static
    inputValue:       word;       // 存储用户输入的4位十六进制值(16位)
    qty5:            byte;       // $5商品数量(3位字段)
    qty4:            byte;       // $4商品数量(3位字段)
    qty3:            byte;       // $3商品数量(3位字段)
    qty2:            byte;       // $2商品数量(3位字段)
    qty1:            byte;       // $1商品数量(3位字段,剩余1位设为保留位)
    totalAmount:     word;       // 最终总金额

begin FastFoodCalculator;

    // 读取用户输入的4位十六进制数
    stdout.put( "输入4位十六进制订单值:" );
    stdin.get( inputValue, 16 );

    // --------------------------
    // 提取各商品数量(按需求调整位段位置,以下为示例分配)
    // 示例位段分配:
    // $5: bits 15-13, $4: bits12-10, $3: bits9-7, $2: bits6-4, $1: bits3-1
    // --------------------------
    // 提取$5商品数量
    mov( inputValue, ax );
    shr( 13, ax );               // 将bits15-13移到低3位
    and( %00000111, ax );        // 掩码保留低3位
    mov( al, qty5 );

    // 提取$4商品数量
    mov( inputValue, ax );
    shr( 10, ax );
    and( %00000111, ax );
    mov( al, qty4 );

    // 提取$3商品数量
    mov( inputValue, ax );
    shr( 7, ax );
    and( %00000111, ax );
    mov( al, qty3 );

    // 提取$2商品数量
    mov( inputValue, ax );
    shr( 4, ax );
    and( %00000111, ax );
    mov( al, qty2 );

    // 提取$1商品数量(示例取bits3-1)
    mov( inputValue, ax );
    shr( 1, ax );
    and( %00000111, ax );
    mov( al, qty1 );

    // --------------------------
    // 计算总金额:数量×对应单价后累加
    // --------------------------
    mov( 0, totalAmount );

    // 累加$5商品金额
    mov( qty5, al );
    mov( 5, bl );
    mul( bl );                   // ax = al * bl
    add( ax, totalAmount );

    // 累加$4商品金额
    mov( qty4, al );
    mov( 4, bl );
    mul( bl );
    add( ax, totalAmount );

    // 累加$3商品金额
    mov( qty3, al );
    mov( 3, bl );
    mul( bl );
    add( ax, totalAmount );

    // 累加$2商品金额
    mov( qty2, al );
    mov( 2, bl );
    mul( bl );
    add( ax, totalAmount );

    // 累加$1商品金额
    mov( qty1, al );
    mov( 1, bl );
    mul( bl );
    add( ax, totalAmount );

    // --------------------------
    // 输出结果
    // --------------------------
    stdout.newln();
    stdout.put( "$5商品数量:", qty5, nl );
    stdout.put( "$4商品数量:", qty4, nl );
    stdout.put( "$3商品数量:", qty3, nl );
    stdout.put( "$2商品数量:", qty2, nl );
    stdout.put( "$1商品数量:", qty1, nl );
    
    stdout.newln();
    stdout.put( "总金额:$", totalAmount, nl );

end FastFoodCalculator;

关键修复说明

  1. 位段提取修正:

    • 使用shr指令将目标位段移动到低字节区域,再通过and掩码操作提取对应位数的有效数值(3位则掩码为%00000111)。
    • 若输入1000(十六进制,二进制0001000000000000)需要对应$1商品数量为1,只需将$1的位段调整到bits12-10,即移位10位后取低3位即可。
  2. 总金额计算逻辑补充:

    • 初始化总金额为0,依次对每个商品的数量乘以对应单价,将结果累加到总金额中,完全符合“数量×单价求和”的业务逻辑。
  3. 输入输出对齐:

    • 明确按十六进制读取输入值,输出时清晰展示各商品数量和最终总金额,便于验证结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 00:20:43