0

我正在尝试获取一个字节的信息,对它们进行真正的编号和比较。我的比较产生了错误的输入。这是代码。

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

static
    dayR: byte;
    monthR: byte;
    yearR: word;

    day: uns8;
    month: uns8;
    year: uns16;

    packedDate: dword;

begin dateDemo;


    stdout.put( "Enter the current month, day, and year: " );
    stdin.get( monthR, dayR, yearR );

    mov( 0, eax );
    mov(0, bx);
    mov( eax, packedDate ); // Just in case there is an error.

    mov(monthR, ah);
    mov(ah, month);
    mov(dayR, al);
    mov(al, day);
    mov(yearR, bx);
    mov(bx, year);

    // Pack the data into the following bits:
    //
    // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    // m m m m d d d d d y y y y y y y

    if( month > 12 ) then
        stdout.put( "Month value is too large", nl );

    elseif( month = 0 ) then
        stdout.put( "Month value must be in the range 1..12", nl );

    elseif( day > 31 ) then
        stdout.put( "Day value is too large", nl );

    elseif( day = 0 ) then
        stdout.put( "Day value must be in the range 1..31", nl );

    elseif( year > 99 ) then
        stdout.put( "Year value must be in the range 0..99", nl );

    else    
        stdout.put("It worked");

        /*mov( month, al );
        shl( 5, ax );
        or( day, al );
        shl( 7, ax );
        or( year, al );
        mov( ax, packedDate );*/

    endif;

我遗漏了一些无关紧要的代码。如果我输入“10 20 1997”,它会输出“月份太大”,我尝试使用 uns8 而不是 hte 字节信息进行​​比较,但无济于事。任何人都可以给我提示。

我的年份也将达到 2000 年,因此它将使用一个完整的 16 位字。

4

1 回答 1

1

环境

  • HLA (High Level Assembler - HLABE back end, POLINK linker) 版本 2.16 build 4413 (prototype)
  • 视窗 10

笔记

  • 此问题中的代码复制自 Randall Hyde 的汇编语言艺术中第 2 章第 11 节“位字段和打包数据”中的 dateDemo 示例。

解决方案

  • demoDate 示例打包和解包日期表示,因此如果您想使用 a作为年份值word,则应将示例更改为使用长打包日期格式。dwordword
  • 长包装日期格式:
|31 . . . . . . . . . . . . . . 16|15 . . . . . . 8|7 . . . . . . 0|
| y y y y y y y y y y y y y y y y | m m m m m m m m|d d d d d d d d|

例子

  • dword下面的代码是为处理长压缩日期格式而更改的 demoDate 示例。
program LongDateDemo;
#include ("stdlib.hhf")

storage
    Day:            uns8;
    Month:          uns8;
    Year:           uns16;
    LongPackedDate: dword;

begin LongDateDemo;
    stdout.put("Enter the current month, day, and year (EX: 6 14 2020): ");
    stdin.get(Month, Day, Year);

    // Long packed date format
    // |31 - 16|15 - 8|7 - 0| 
    // |year   |month |day  |

    mov(0, EAX);
    mov(EAX, LongPackedDate ); // Just in case there is an error.

    if( Month > 12 ) then
        stdout.put( "Month value is too large", nl );

    elseif( Month = 0 ) then
        stdout.put( "Month value must be in the range 1..12", nl );

    elseif( Day > 31 ) then
        stdout.put( "Day value is too large", nl );

    elseif( Day = 0 ) then
        stdout.put( "Day value must be in the range 1..31", nl );

    elseif( Year > 65535 ) then
        stdout.put( "Year value must be in the range 0..65535", nl );

    else    
        mov(Year, AX);
        shl(8, EAX);
        or(Month, AL);
        shl(8, EAX);
        or(Day, AL);
        mov(EAX, LongPackedDate);
    endif;

    // Okay, display the packed value:

    stdout.put("Packed data = $", LongPackedDate, nl);

    // Unpack the date:

    mov(LongPackedDate, EAX);
    and($FF, AL);   // Retrieve the day value.
    mov(AL, Day);

    mov(LongPackedDate, EAX);
    shr(8, EAX);
    and($FF, AL);   // Retrieve the month value.
    mov(AL, Month);

    mov(LongPackedDate, EAX);
    shr(16, EAX);
    and($FFFF, AX); // Retrieve the year value.
    mov(AX, Year);

    stdout.put("The date is: ", Month, "/", Day, "/", Year, nl);

end LongDateDemo;
于 2020-06-14T14:45:39.833 回答