关于小程序打包位的问题

时间:2011-08-29 08:09:11

标签: assembly x86

我正在学习使用汇编语言(特别是HLA)打包数据,这是一个足够简单的概念。但是,在下面的演示程序(缩写为实际版本)中,我不明白为什么0被移入ax?

static
day: uns8;
month: uns8;
year: uns8;
packedDate: word;

begin test1;
stdout.put( "Enter the current month, day, and year: " );
stdin.get( month, day, year );
mov( 0, ax );
mov( ax, packedDate ); // Just in case there is an error.
mov( month, al );
shl( 5, ax );
or( day, al );
shl( 7, ax );
or( year, al );
mov( ax, packedDate );
// Okay, display the packed value:
stdout.put( "Packed data = $", packedDate, nl );
//unpack the date
mov( packedDate, ax );
and( $7f, al ); // Retrieve the year value.
mov( al, year );
mov( packedDate, ax ); // Retrieve the day value.
shr( 7, ax );
and( %1_1111, al );
mov( al, day );
mov( packedDate, ax ); // Retrieve the month value.
rol( 4, ax );
and( %1111, al );
mov( al, month );
stdout.put( "The date is ",month, "/", day, "/",year, nl );
end test1;

删除该行似乎没有任何效果。将0移入ax,然后将ax移入packedDate的目的是什么?

平台是Win764

1 个答案:

答案 0 :(得分:0)

将0移动到packedDate这里没有做任何事情,因为你有通知,但我猜这样做是为了让代码更容易扩展并进行错误检查。目前,代码不会检查无效日期,如果代码被修改为0 packedDate将表示无效日期。

另一方面,将0移入ax需要在执行以下代码之前清除8个最高有效位(ah):

mov( month, al );
shl( 5, ax ); 

如果ah不清楚,您仍然可能在之前的操作中遗留了垃圾位。代码也可以写成:

movzx( month, ax );
shl( 5, ax );