如何处理以字节为单位的数字

问题描述 投票:0回答:1

我试图接受一个字节的信息,真正的数字和比较他们.我的比较正在生成错误的输入。下面是代码。

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代替h字节信息进行比较,但没有用。谁能给我一些提示。

另外,我的年份将上升到2000年,所以它将使用整个16位的字。

assembly error-handling compilation hla
1个回答
1
投票

ENVIRONMENT

  • HLA(高级汇编器-HLABE后端,POLINK链接器)2.16版build 4413(原型)
  • Windows 10

  • 本题中的代码复制自Randall Hyde的《汇编语言的艺术》中第2章第11节 "比特字段和打包数据 "中的dateDemo示例。

解答:本题的代码抄自Randall Hyde的《汇编语言的艺术》中第2章第11节,比特字段和打包数据中的例子。

  • demoDate示例打包和解压了一个叫做 word 日期表示法,因此这个例子应该改变为使用 dword 长包装日期格式,如果你想使用的是 word 为您的年份值。
  • 长包装日期格式。
|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|

例子

  • 下面的代码是demoDate的例子,它被修改为处理了 dword 长包装日期格式。
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;
© www.soinside.com 2019 - 2024. All rights reserved.