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

标签 assembly error-handling compilation hla

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

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 位字。

最佳答案

环境

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

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

  • 解决方案
  • demoDate 示例打包和解包 word日期表示,因此应将示例更改为使用 dword如果您想使用 word 的长压缩日期格式为您的年份值(value)。
  • 长包装日期格式:
  • |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;
    

    关于assembly - 如何处理以字节为单位的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472195/

    相关文章:

    assembly - 为什么对非核心响应/非核心请求的需求过多?

    asp.net-mvc - 如何处理mvc中的渲染异常?

    java - 请帮助我让Java命令正常工作

    debugging - 如何修复创建 MPI 派生数据类型期间的无效参数

    c - ELF 如何填充它的 `short`?

    c++ - 寄存器流水线计算

    assembly - System V ABI - AMD64 - GCC 发出的程序集中的堆栈对齐

    haskell - 处理 `ExceptT` 计算中的不同错误类型

    C# 异常处理与方法结果返回错误

    iphone - 在 xCode 中加速编译