assemblyx86hla

Don't understand shifting, rotating, and LAHF


For example, this is the assignment I have

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

I was able to get the source code of the answer but I'm having trouble understanding.

I put my thought process inside the comments

 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;

We're also not allowed to use loops yet.

I guess I don't understand the shl and the LAHF part

From what I understand, LAHF means Load AH from Flags. So this puts flags into AH. Makes sense that that is where AH is coming from then. Also, shl puts a 0 into bit 0 and then carries over what was in bit 7 into a carry flag. But I'm just not sure what that means.


Solution

  • lahf just loads the cpu flags into the upper byte of the ax regishter (ah). Bit 0 of the flags (and ah after a lahf instruction) is the carry flag. So, if the msb of bh was 1 then after a left shift the carry would be set. Basically this is just popping off bits from msb to lsb from bh.