1

I have this following assignment: Write an HLA Assembly program that prompts for an int8 value to inspect and then prints it in binary format. For example, here would be the program output for various entered values

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

(Hint: There is no standard output that prints in binary output, so you need to do this yourself. In order to accomplish this, you need to move a bit at time into the carry flag and print 0 or 1, depending on what you find in the Carry bit. Shift and repeat this procedure 8 times and you are done! Eventually, we will learn how to loop, making this task much less terrible.)

(Second Hint:LAHF pushes the Carry Bit and all the other flags out of the EFLAGS register and into AH. As an Assembly programmer, you have the power to mask out all the bits but the one you are interested in by using either AND or OR.) Here is what I have currently learned in the class: http://homepage.smc.edu/stahl_howard/cs17/FileManager/referenceguides/referenceguideii.htm My code is this so far, and I believe it is a logic error, because regardless of what number I put in I just get a string of 16 0's.

 begin program BinaryOutput;
 #include( "stdlib.hhf" );
 static
   iDataValue : int8;  // the value to inspect
 begin BinaryOutput;

    stdout.put( "Gimme a decimal value to print: ", nl);
    stdin.get( iDataValue );
    mov(0, BH);
    mov( iDataValue, BH);

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


    shl(1, BH); //1st
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

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

    shl(1, BH); //3rd
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

    shl(1, BH); //4th
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

    shl(1, BH); //5th
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

    shl(1, BH); //6th
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

    shl(1, BH); //7th
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);

    shl(1, BH); //8th
    lahf();
    and( %0000_0001, AH );
    mov(AH, BH);
    stdout.putb(BH);





 end BinaryOutput;
4

2 回答 2

1

一个明显的错误是您正在覆盖BH. 这样的事情应该会更好:

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

对其他人重复,或使用循环;)不确定putb使用什么格式,因为你提到得到 16 个零,我猜它可能是写 2 个十六进制数字。在这种情况下,请检查您是否有不同的输出功能(也许puti8?)。如果没有打印单个数字,则打印字符(您必须通过添加'0'或转换为 ascii '1')。

于 2015-10-12T16:53:28.527 回答
-1
stdout.puti8( BH );
stdout.put( " in binary is: %" );

    shl(1, BH); 
    lahf();
    //load AH with Flags
    and( %0000_0001, AH );
    //passing an arugement to AH[0]
    stdout.puti8( AH ); 
    //without i8, it outpouts 2 digits

这输出个位数,我必须做同样的任务,这使它工作。

于 2021-03-27T21:13:44.123 回答