Suppose the content of the register al equals to 4Ah. What will be the content of ax after performing the following code? start: push cx mov ch,al and al,0fh add al,30h daa cmp al,3ah jc binhe0 inc al binhe0: mov ah,al mov al,ch mov cl,4 shr al,cl and al,0fh add al,30h daa cmp al,3ah jc binhe1 inc al binhe1: pop cx ret
I tried the code myself because I'm unfamiliar with the DAA opcode that deals with Binary Coded Decimal operations, something that nobody uses anymore ^^. I'll detail line by line what's happening: push cx ; Keep old CX because we'll modify it mov ch,al ; Copy the value 4Ah to CH and al,0fh ; Isolate the 4 lowest bits of AL => 0Ah add al,30h ; Gives us 0Ah + 30h = 3Ah daa ; This nasty instruction detects the 4 lowest bits (0Ah) encode ; the value 10 which, in Binary Coded Decimal (BCD), overflows the digit ; Indeed, a decimal digit can only encode from 0 to 9. ; 10 means 0 + carry to the next digit, which is exactly what this ; instruction is doing: it will make AL = 40h cmp al,3ah ; Here, always think of a comparison as a subtraction, so it subtracts ; 40h - 3Ah, which doesn't set the carry bit jc binhe0 ; so we don't jump... inc al ; and increment AL which becomes 41h binhe0: mov ah,al ; duplicate 41h in the top byte, giving AX = 4141h mov al,ch ; restore the original AL that we had kept in CH => AX = 414Ah mov cl,4 ; CL=4 shr al,cl ; Shift AL to the righ by 4 bits. AL = 04h (and AX=4104h) and al,0fh ; Isolate the 4 lowest bits (but that's useless since the SHR already did ; that for us). Anyway, AL still = 04h add al,30h ; Gives us AL = 04h + 30h = 34h daa ; Another BCD adjustment but this time there is no digit overflow ; (i.e. no digit greater than 9 that needs to carry) so AL=34h cmp al,3ah ; As I said earlier, imagine a comparison as a subtraction that doesn't store the result... ; So here we subtract 34h - 3Ah, this gives a negative number ; and also sets the carry flag jc binhe1 ; so since the carry is set, we jump inc al ; Not executed binhe1: pop cx ; We restore the original CX ret ; Returns victorious... So finally, the value of AX = 4134h ... And that code is completely useless anyway! But don't say that to your teacher! ^^
Join our real-time social learning platform and learn together with your friends!