Todo
C++ dispone de la opción de poder escribir directamente instrucciones en lenguaje ensamblador junto con el resto del código fuente. Para esto dispone de una palabra clave específica: asm.
La palabra asm indica que la cadena literal que sigue será incluida en el código objeto en la posición indicada.
La sintaxis empleada depende del compilador. En el caso de C++Builder,es la siguiente:
asm <opcode> <operandos> <; o NL>
Ejemplo
asm pop dx;
asm push ebx;
asm call @@std@%basic_ostream$c19std@%char_traits$c%%@put$qc;
asm add esp,8;
asm push ebx;
Se permite que varias instrucciones ensamblador pueden ser agrupadas en un solo bloque precedido por el indicador asm. Ejemplo:
asm {
mov ax, 0x0e07;
xor bx, bx;
int 0x10;
}La sintaxis empleada por el compilador C++ GNU para Windows utilizado por Dev-C++ no utiliza la sintaxis de Intel, sino la de AT&T.
Ejemplo [2]:
int AdrIO; // variable global
static char ValIO; // variable global
void foo() {
__asm("mov _AdrIO, %dx") ; // cargar un registro de 16 bits
__asm("mov _ValIO, %al") ; // cargar un registro de 8 bits
__asm("mov %ax,%dx") ; //mover contenido registro AX a DX
}
Nota: no olvidar incluir el guión bajo ("underscore") precediendo los nombres de variables globales.
Ni que decir tiene que cualquier código ensamblador insertado mediante asm, es específico, y por tanto dependiente, de la plataforma sobre la que se ejecutará el programa. Este tipo de sentencias deben reducirse al máximo si queremos facilitar la portabilidaddel código.
8086 assembler tutorial for beginners (part 6)
Arithmetic and Logic Instructions
Most Arithmetic and Logic Instructions affect the processor status register (or Flags)
As you may see there are 16 bits in this register, each bit is called a flag and can take a value of 1 or 0.
* Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. Forexample when you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow this flag is set to 0.
* Zero Flag (ZF) - set to 1 when result is zero. For none zero result this flag is set to 0.
* Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to 0. Actually this flag take the value of the most significant bit.
* Overflow Flag (OF) -set to 1 when there is a signed overflow. For example, when you add bytes 100 + 50 (result is not in range -128...127).
* Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result, and to 0 when there is odd number of one bits. Even if result is a word only 8 low bits are analyzed!
* Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for lownibble (4 bits).
* Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from external devices.
* Direction Flag (DF) - this flag is used by some instructions to process data chains, when this flag is set to 0 - the processing is done forward, when this flag is set to 1 the processing is done backward.
There are 3 groups of instructions.
Firstgroup: ADD, SUB,CMP, AND, TEST, OR, XOR
These types of operands are supported:
REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
After operation between operands, result is always stored in firstoperand. CMP and TEST instructions affect flags only and do not store a result (these instruction are used to make decisions during program execution).
These instructions affect these flags only:
CF, ZF, SF, OF, PF, AF.
* ADD - add second operand to first.
* SUB - Subtract second operand to first.
* CMP - Subtract second operand from first for flags only.
* AND - Logical AND between...
Regístrate para leer el documento completo.