Transcript UPPBYGGNAD

PC-technology
MASM and Inline Code in C
MASM and Inline Code in C
• Some things to think about
– Which size has the register you use
•
•
•
•
•
AX, AH, AL
MOV AX, 500 Max 16 bits
MOV AH, 5 Max 8 bits
MOV AL,2
Max 8 bits
Now AX holds 0502H (1282 decimal)
– OUT always uses the accumulator, AX
• in older versions of processors, only lower byte, AL
PC-Teknik © CAAK
2
MASM and Inline Code in C
• Some things to think about, cont.
– MUL always works on AX
• MUL BL
• MUL BX
BL*AL the result in AX
BX*AX the result in DX:AX
– DX*65536+AX
because DX holds the 16 higher bits
and AX the lower 16 bits
– When working with .exe-files, the code and
data have to be placed in separate segments
PC-Teknik © CAAK
3
One solution to the Organ
• Make an array of the numbers to divide the
clock frequency with:
– TONE DW 0, 0E24h, 11D1h, 0, 0, 10D0h, 0EFBh,0
– The first one is used when character A is
pressed, the next, the 0E24H when B is pressed,
and 11D1H gives the tone C when C is pressed.
PC-Teknik © CAAK
4
One solution to the Organ
• How do you get the right address?
– Ex. You press the C and want the tone C to sound.
C has the ASCII code 67 and A has the code 65
• If you take ‘A’-65 you get 0
• If you take ‘C’-65 you get 2
• When you go into the array the first number in the
array is at address TONE + 0, the next at address
TONE+2, the next at TONE+4 =>
if C is pressed you want to goto address 2*2 which is
(‘C’-65)*2
MOV BH,0
SUB BX,'A'
ADD BX,BX
;empty high byte of BX
;ASCII-code - 65
; (ASCII code-65) * 2
5
One solution to the Organ
• How do you get the right address?
– To point at address TONE:
• MOV SI,OFFSET TONE
– To point at the first byte of number
corresponding to ‘C’
– MOV AL,DS:[SI+BX]
PC-Teknik © CAAK
6
Inline Code in C
• Links to get help with Inline code:
– http://www.microsoft.com/
• in Swedish: http://www.microsoft.com/sverige/
– use the search function on Inline Assembly
• Links to other assembly language topics
• http://www.jorgon.freeserve.co.uk/
• http://home6.swipnet.se/~w-61969/asm/ASMNY.htm (in Swedish)
• http://www.jegerlehner.ch/intel/
• http://brianvictor.8m.com/
7
Inline Code in C
• Usage
– need a piece of program that runs faster than
the C-code
– and want to use C code to type things onto the
screen
– want to use the data segment in the same way
as in C
• Examples at MSDN
8
# include <iostream.h>
void main(void)
{
unsigned short number1, number2, regAX, regDX;
long int product;
number1=500;
number2=500;
_asm
{
mov ax,number1
mov bx,number2
mul bx
mov regAX,ax
mov regDX,dx
}
cout<<regAX<<endl;
cout<<regDX<<endl;
product = regAX+65536*(long)regDX;
cout<<product;
}
9
/* POWER2.C */
#include <stdio.h>
int power2( int num, int power );
void main( void )
{
printf( "3 times 2 to the power of 5 is %d\n", \power2( 3, 5) );
}
int power2( int num, int power )
{
__asm
{
mov eax, num ; Get first argument
mov ecx, power ; Get second argument
shl eax, cl ; EAX = EAX * ( 2 to the power of CL )
}
/* Return with result in EAX */
}
10