将 32 位文件大小与 16 位用户输入进行比较? [英] Comparing a 32 bit file size to 16 bit user input?

查看:30
本文介绍了将 32 位文件大小与 16 位用户输入进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个汇编程序,它分析文件并将它们的信息(名称和大小)输出到文件中.我基本上使用 4Eh 和 4Eh 中断来搜索文件:它们都返回 DTA 对象,其中包含有关该文件的相关信息.

I'm writing an assembler program, which analyzes files and outputs their information ( name and size ) to the file. I'm basically using 4Eh and 4Eh interrupts to search for files : they both return DTA object with according information about that file in it.

它返回一个 32 位文件大小,我必须将它与 16 位用户输入大小进行比较(如果文件信息大于给定的用户输入大小,我必须只输出文件信息).

It returns a 32-bit filesize which I have to compare with a 16-bit user inputted size (I have to only output files information if it's larger than the given - user inputted - size).

这里明显的问题是我不知道如何将 32 位数字转换为 16 位数字,否则我无法比较它们.也许有人知道如何执行这种转换,或者对如何实施这种比较有任何其他想法?

The obvious problem here is that I don't know how to convert a 32 bit number into a 16 bit number, because otherwise I can't compare them. Maybe someone knows how to perform this conversion or has any other ideas on how to implement this comparison ?

如果可能,也许有人可以提供 TASM Intel x86 程序集的示例代码

If possible, maybe someone could provide an example code in TASM Intel x86 assembly

推荐答案

你的想法倒退了.

您想将 16 位数字零扩展为 32,并进行 32 位比较.(在 C 中,对不同类型的操作会提升为一种足够宽的类型以容纳它们,因为这几乎总是您想要的.)

You want to zero-extend the 16-bit number to 32, and do a 32-bit comparison. (In C, operations on different types promote to a type that's wide enough to hold them both, because that's almost always what you want.)

正如 Jester 解释的那样,这意味着您的用户输入的上半部分始终为零,因此进行扩展精度/BigInteger 比较的上半部分只是检查文件大小的上半部分是否为非零.然后,如果文件大小足够小以至于可能低于 16 位用户输入,请将下半部分与阈值进行比较.

As Jester explains, this means the upper 16 of your user input is always zero, so doing the upper half of the extended-precision / BigInteger comparison is simply checking that the upper half of the file size for non-zero. Then, if the file size is small enough that it might be below a 16-bit user input, compare the low half with the threshold.

您还可以通过进行扩展精度减法来仅使用一条分支指令检查小于:

You can also check for less-than with only one branch instruction by doing an extended-precision subtract:

file_loop:               ; do {
    load a new file size

    ;; file size in dx:ax,  threshold in cx
    cmp    ax, cx        ; low half (non-destructive: cmp = sub but without writing ax)
    sbb    dx, 0         ; high half. (destructive)
    ; ZF reflects only the high-half compare result
    ; CF is useful (and so are SF/OF for signed compare).
    ; Avoid JCC conditions that depend on ZF, like JA.
    jnb   file_loop    ; } while(CF==0);   (JNB = JNC)

;;; dx:ax < 0:cx unsigned was true.
;;; So we know the original DX was 0 (and is now 0xFFFF)
;;; AX = the file size (which we didn't destroy), and we know it fits in 16 bits.

    do something with this file
    jmp  file_loop

这很好而且很紧凑,但可能并不比 cmp/jzcmp/jb 好,尤其是当 32 位数字在内存中时.(您不想将 sbb 与内存目标一起使用;那样效率较低.)

This is nice and compact, but probably not particularly better than cmp/jz and cmp/jb, especially if the 32-bit number is in memory. (You don't want to use sbb with a memory destination; that's less efficient.)

转换 32->16 是微不足道的:只需通过忽略高 16 位来截断.但你似乎已经意识到这不会做你想做的事.

Converting 32->16 is trivial: just truncate by ignoring the upper 16 bits. But you seem to have already realized that wouldn't do what you want.

要回答你的旧标题:将 dx:ax 中的 32 位整数转换为 cx 中的 16 位整数,请使用以下指令:mov cx, ax.

To answer your old title: to convert a 32-bit integer in dx:ax to a 16-bit integer in cx, use this instruction: mov cx, ax.

这篇关于将 32 位文件大小与 16 位用户输入进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆