Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a maximum of two instructions to clear, set and complement some bits in the AL register

You are required to write a maximum of two instructions in assembly to do the following:

  1. Clear bits 0 and 7 of register AL, i.e. make them 0
  2. Set bits 3 and 4 of register AL, i.e. make them 1.
  3. Complement bits 1 and 5 of register AL.
  4. Keep all other bits in the register AL as is without changing their values.
like image 596
Nick Avatar asked Apr 26 '10 18:04

Nick


People also ask

What is the use of AND al 0FH instruction in display procedure?

The AND operation can be used for clearing one or more bits. For example, say the BL register contains 0011 1010. If you need to clear the high-order bits to zero, you AND it with 0FH.

What is Al register?

The least significant byte of AX can be used as a single 8-bit register called AL, while the most significant byte of AX can be used as a single 8-bit register called AH. These names refer to the same physical register.

What are EAX EBX ECX EDX registers?

The EAX, EBX, ECX, EDX, EBP, EDI, and ESI registers are all 32-bit general-purpose registers, used for temporary data storage and memory access. Some of CPU instructions modify specific registers.

What is AH AND AL in assembly language?

al and ah are the 8-bit, "char" size registers. al is the low 8 bits, ah is the high 8 bits. They're pretty similar to the old 8-bit registers of the 8008 back in 1972.


2 Answers

The trick here is to do the following:

  1. use an OR instruction to set bits 0, 3, 4 and 7

  2. use an XOR instruction to complement bits 0, 1, 5 and 7

Note that bits 0 and 7 first get set in (1) and then cleared in (2).

I'll leave the actual asm instructions to you, since this is your homework, after all.

like image 199
Paul R Avatar answered Nov 08 '22 06:11

Paul R


One DB instruction defining an array of 256 "result" values, and one move instruction to move an element of this array into al, using the current value in al as an index.

Wouldn't that work ?

It might even be argued that this is in fact even a single-instruction solution, since the DB is not really an instruction that executes at run-time, rather it is a compile-time declarative.

like image 41
Erwin Smout Avatar answered Nov 08 '22 06:11

Erwin Smout