我无法在我编写的程序中找到错误所在。 我需要在输入 1240 时打印出来。 给我 4 个十六进制数字:1240 1 1 美元商品 1 件 2 美元商品 1 件 3 美元商品 0 4 美元商品 0 5 美元商品 订单总成本:6 美元
这是我执行时得到的结果 请以 4 个十六进制数字形式向我提供您的订单:1240 0 $1 商品 3 2 美元商品 3 3 美元商品 2 件 4 美元商品 0 5 美元商品 订单总成本:0 美元
我也不知道如何让我的程序将此 76C9 作为输入,并且必须打印出来 给我 4 个十六进制数字:76C9 7 1 美元商品 3 2 美元商品 3 3 美元商品 1 件 4 美元商品 1 件 5 美元商品 订单总成本:31 美元
当我尝试运行它时,我收到一个转换错误,我理解它是因为该程序只接受整数,这就是它导致错误的原因,但我真的不知道如何让程序接受输入。
非常感谢任何帮助!!
这是我当前的代码 `
program DollarValueMenu;
#include ("stdlib.hhf");
static
orderValue: int16;
begin DollarValueMenu;
stdout.put("Feed me your order as 4 hex digits: ");
stdin.get(orderValue);
mov(orderValue, ax);
// Initialize dx as the total cost accumulator
mov(0, dx);
// Extracting $1 items
mov(ax, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $1 item");
stdout.newln();
// Compute cost for $1 items by adding cx
add(dx, cx);
// Extracting $2 items
mov(ax, cx);
shr(3, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $2 item");
stdout.newln();
// Compute cost for $2 items
mov(bx, cx);
add(bx, bx);
add(dx, bx);
// Extracting $3 items
mov(ax, cx);
shr(6, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $3 item");
stdout.newln();
// Compute cost for $3 items
mov(bx, cx);
add(bx, bx);
add(bx, cx);
add(dx, bx);
// Extracting $4 items
mov(ax, cx);
shr(9, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $4 item");
stdout.newln();
// Compute cost for $4 items
shl(2, cx);
add(dx, cx); // Add cost of $4 items to total
// Extracting $5 items
mov(ax, cx);
shr(12, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $5 item");
stdout.newln();
// Compute cost for $5 items
mov(bx, cx); // Move count to bx
shl(2, bx);
add(bx, cx);
add(dx, bx); // Add cost of $5 items to total
// Output the total cost
stdout.put("Total Order Costs: $");
stdout.puti16(dx); // Output total cost
stdout.newln();
end DollarValueMenu;
这是我执行时得到的结果 请以 4 个十六进制数字形式向我提供您的订单:1240 0 $1 商品 3 2 美元商品 3 3 美元商品 2 件 4 美元商品 0 5 美元商品 订单总成本:0 美元
stdout.put("Feed me your order as 4 hex digits: "); stdin.get(orderValue); mov(orderValue, ax);
我不是高级汇编器的常规用户,但我似乎记得“当您直接读入寄存器时,HLA 以十六进制格式读取”。
stdout.put("Feed me your order as 4 hex digits: ");
stdin.get( ax );
最好在输入前加上 $ 字符作为前缀,以表明后面的内容确实是十六进制。
请输入 4 个十六进制数字:1240 1 $1 商品 1 $2 商品 1 $3 商品 0 $4 商品 0 $5 商品 总订单成本:$6
给我 4 个十六进制数字:76C9 7 $1 项目 3 $2 项目 3 $3 项目 1 $4 项目 1 $5 项目总订单成本:$31
从这些例子中可以清楚地看出预期的结果。遗憾的是,您当前的程序包含两个基本错误:
考虑这个片段:
mov(bx, cx); // Move count to bx shl(2, bx); add(bx, cx); add(dx, bx); // Add cost of $5 items to total
在 HLA 中,源是最左边的操作数,目标是最右边的操作数。通过查看像
shl(2, bx)
这样的指令,您可以轻松记住这一点,其中最左边的操作数(立即数)根本不可能成为任何内容的目的地。那必须是源操作数。
mov(cx, bx); // Move count to bx
shl(2, bx);
add(cx, bx);
add(bx, dx); // Add cost of $5 items to total in DX
考虑十六进制数字 $1240 和 $76C9:
$1240 == %0001'0010'0100'0000 == %0'001'001'001'000'000
$76C9 == %0111'0110'1100'1001 == %0'111'011'011'001'001
^ ^ ^ ^ ^
| | | | + $5 items
| | | + $4 items
| | + $3 items
| + $2 items
+ $1 items
代码应该看起来更像这样:
stdin.get( ax );
// Extracting $1 items
shl(1, ax); // drop the 16th bit (unused)
rol(3, ax); // brings the triplet of interest into the lowest bits
mov(ax, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $1 item");
stdout.newln();
// Add cost for $1 items by moving CX to DX
mov(cx, dx);
// Extracting $2 items
rol(3, ax); // brings the triplet of interest into the lowest bits
mov(ax, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $2 item");
stdout.newln();
// Add cost for $2 items
add(cx, dx);
add(cx, dx);
// Extracting $3 items
rol(3, ax); // brings the triplet of interest into the lowest bits
mov(ax, cx);
and(7, cx);
stdout.puti16(cx);
stdout.put(" $3 item");
stdout.newln();
// Add cost for $3 items
add(cx, dx);
add(cx, dx);
add(cx, dx);
从这里继续应该很容易......