-2

例如,这是我的作业

编写一个 HLA 汇编程序,提示输入一个 int8 值进行检查,然后以二进制格式打印它。例如,这里将是各种输入值的程序输出

Gimme a decimal value to print: 15
15 is 0000_1111
Gimme a decimal value to print: 7
7 is 0000_0111

我能够获得答案的源代码,但我无法理解。

我把我的思考过程放在评论里

 program binaryoutput;
 #include( "stdlib.hhf" );
 static
   zGivenNumber : int8;  // the value to inspect
 begin binaryoutput;
    //Ask for a decimal number
    stdout.put( "Gimme a decimal value to print: ");
    //Put that number in 'zGivenNumber' (Let's say 7)
    stdin.get( zGivenNumber );
    //Put 'zGivenNumber' inside 'BH' ('BH' now contains 7)
    mov( zGivenNumber, BH);

    stdout.put("Number in binary is: ", nl);

    //Shift left 1 at 'BH' (This makes 'BH' 14)
    shl(1, BH);
    //Not a clue what this is doing
    lahf();
    //Checking to see if 0000_0001 and AH match 0's and 1's
    //(I'm not sure about the % sign as well as where AH came from)
    and( %0000_0001, AH );
    //Print out 'AH' in Eight Bit form
    stdout.puti8(AH);

    shl(1, BH); //2
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);

    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);
    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);
    stdout.put("_");
    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);
    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);
    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);
    shl(1, BH); //Next
    lahf();
    and( %0000_0001, AH );
    stdout.puti8(AH);

 end binaryoutput;

我们还不允许使用循环。

我想我不明白 shl 和 LAHF 部分

据我了解,LAHF 表示从标志加载 AH。因此,这会将标志放入 AH。有道理,这就是 AH 的来源。此外, shl 将 0 放入位 0,然后将位 7 中的内容转移到进位标志中。但我只是不确定这意味着什么。

4

2 回答 2

3

lahf只需将 cpu 标志加载到ax寄存器 ( ah) 的高字节。标志的第 0 位(以及ahlahf指令之后)是进位标志。因此,如果 msbbh为 1,则在左移后将设置进位。基本上,这只是从 msb 到 lsb 从bh.

于 2016-01-16T00:16:02.003 回答
0

https://stackoverflow.com/tags/x86/info标签 wiki 有指向英特尔指令参考手册的链接,以及许多其他好东西。如果你不知道 LAHF 是做什么的,为什么不直接查一下呢?

此外,您可以直接使用进位标志,而不是弄乱 LAHF。(x86 移位指令集 CF = 最后一位移出,您在 HLA 注释中未提及。)

shl  bh, 1
setc al
add  al, '0'   ; AL = '0' or '1', depending on the last bit shifted out of bh

或者

xor  eax,eax    ; have to zero AL every time, but if you want to get fancy you can then use AL and AH, and then store two digits from AX.
shl  bh, 1
adc  al, '0'    ; AL = '0' + the last bit shifted out of BH

进位标志是特殊的,并且有像 /// 这样的指令来adc与之交互,以及通常的.sbbrcrrclsetcc

于 2016-01-16T00:32:23.627 回答