assemblybinarydecimallow-levelhla

Decimal to Binary in High Level Assembly (HLA)


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;

Solution

  • One obvious mistake is that you are overwriting BH. Something like this should work better:

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

    Repeat for the others, or use a loop ;) Not sure what format putb uses, since you mentioned getting 16 zeroes, I guess it may be writing 2 hex digits. In that case, check if you have a different output function (maybe puti8?). If none prints single digit then print chararacters (you will have to convert to ascii then by adding '0' or '1').