# منتديات طلاب الجامعات الأردنية > منتدى علم الحاسوب CS >  أمثلة لمادة المايكرو

## شذى البنفسج

اسم المثال مكتوب بالبداية عند ال TITLE ..





TITLE Add and Subtract, Version 2 (AddSub2.asm)
; This program adds and subtracts 32-bit integers
; and stores the sum in a variable.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
val1 dword 10000h
val2 dword 40000h
val3 dword 20000h
finalVal dword ?
.code
main PROC
mov eax,val1 ; start with 10000h
add eax,val2 ; add 40000h
sub eax,val3 ; subtract 20000h
mov finalVal,eax ; store the result (30000h)
call DumpRegs ; display the registers
exit
main ENDP
END main



------------------------------------------


TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs
exit
main ENDP
END main 

-------------------------------------------------

------------

TITLE Add and Subtract (AddSubAlt.asm)
; This program adds and subtracts 32-bit integers.
; 32-bit Protected mode version
; Last update: 2/1/02
.386
.MODEL flat,stdcall
.STACK 4096
ExitProcess PROTO,dwExitCodeWORD
DumpRegs PROTO
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs
INVOKE ExitProcess,0
main ENDP
END main
----------------------------------------------------------

TITLE Add and Subtract (AddSubR.asm)
; This program adds and subtracts 32-bit integers.
; 16-bit Real mode version.
; Last update: 2/1/02
.MODEL small,stdcall
.STACK 4096
.386
DumpRegs PROTO
.code
main PROC
mov ax,@data ; data seg address
mov ds,ax ; copy to DS
mov es,ax ; copy to ES
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs
mov ah,4Ch ; exit process
mov al,0 ; return code = 0
int 21h ; call MS-DOS function
main ENDP
END main

------------------------------------------------

نموذج :

TITLE Program Template (template.asm)
; Program De******ion:
; Author:
; Date Created:
; Last Modification Date:
INCLUDE Irvine32.inc
; (insert symbol definitions here)
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit ; exit to operating system
main ENDP
; (insert additional procedures here)
END main
-----------------------------------------

TITLE Addition and Subtraction (AddSub3.asm)
; Chapter 4 example. Demonstration of ADD, SUB,
; INC, DEC, and NEG instructions, and how
; they affect the CPU status flags.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
Rval SDWORD ?
Xval SDWORD 26
Yval SDWORD 30
Zval SDWORD 40
.code
main PROC
; INC and DEC
mov ax,1000h
inc ax ; 1001h
dec ax ; 1000h
; Expression: Rval = -Xval + (Yval - Zval)
mov eax,Xval
neg eax ; -26
mov ebx,Yval
sub ebx,Zval ; -10
add eax,ebx
mov Rval,eax ; -36
; Zero flag example:
mov cx,1
sub cx,1 ; ZF = 1
mov ax,0FFFFh
inc ax ; ZF = 1
; Sign flag example:
mov cx,0
sub cx,1 ; SF = 1
mov ax,7FFFh
add ax,2 ; SF = 1
; Carry flag example:
mov al,0FFh
add al,1 ; CF = 1, AL = 00
; Overflow flag example:
mov al,+127
add al,1 ; OF = 1
mov al,-128
sub al,1 ; OF = 1
exit
main ENDP
END main
------------------------------------

TITLE Copying a String (CopyStr.asm)
; This program copies a string.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0),0
.code
main PROC
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
L1:
mov al,source[esi] ; get a character from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1 ; repeat for entire string
exit
main ENDP
END main
-------------------------------------------

TITLE Data Transfer Examples (Moves.asm)
; Chapter 4 example. Demonstration of MOV and
; XCHG with direct and direct-offset operands.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
val1 WORD 1000h
val2 WORD 2000h
arrayB BYTE 10h,20h,30h,40h,50h
arrayW WORD 100h,200h,300h
arrayD DWORD 10000h,20000h
.code
main PROC
; MOVZX
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
; MOVSX
mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
movsx cx,bl ; CX = FF9Bh
; Memory-to-memory exchange:
mov ax,val1 ; AX = 1000h
xchg ax,val2 ; AX = 2000h, val2 = 1000h
mov val1,ax ; val1 = 2000h
; Direct-Offset Addressing (byte array):
mov al,arrayB ; AL = 10h
mov al,[arrayB+1] ; AL = 20h
mov al,[arrayB+2] ; AL = 30h
; Direct-Offset Addressing (word array):
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
; Direct-Offset Addressing (doubleword array):
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h
exit
main ENDP
END main

-----------------------------------------------

TITLE Operators (Operator.asm)
; Demonstration of TYPE, LENGTHOF, SIZEOF operators
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
byte1 BYTE 10,20,30
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE '12345678',0
myArray BYTE 10,20,30,40,50,
60,70,80,90,100
; You can examine the following constant values
; by looking in the listing file (Operator.lst):
;---------------------------------------------
X = LENGTHOF byte1 ; 3
X = LENGTHOF array1 ; 30 + 2
X = LENGTHOF array2 ; 5 * 3
X = LENGTHOF array3 ; 4
X = LENGTHOF digitStr ; 9
X = LENGTHOF myArray ; 10
X = SIZEOF byte1 ; 1 * 3
X = SIZEOF array1 ; 2 * (30 + 2)
X = SIZEOF array2 ; 2 * (5 * 3)
X = SIZEOF array3 ; 4 * 4
X = SIZEOF digitStr ; 1 * 9
.code
main PROC



exit
main ENDP
END main

--------------------------------------------

TITLE Pointers (Pointers.asm)
; Demonstration of pointers and TYPEDEF.
; Last update: 2/1/02
INCLUDE Irvine32.inc
; Create user-defined types.
PBYTE TYPEDEF PTR BYTE ; pointer to bytes
PWORD TYPEDEF PTR WORD ; pointer to words
PDWORD TYPEDEF PTR DWORD ; pointer to doublewords
.data
arrayB BYTE 10h,20h,30h
arrayW WORD 1,2,3
arrayD DWORD 4,5,6
; Create some pointer variables.
ptr1 PBYTE arrayB
ptr2 PWORD arrayW
ptr3 PDWORD arrayD
.code
main PROC
; Use the pointers to access data.
mov esi,ptr1
mov al,[esi] ; 10h
mov esi,ptr2
mov ax,[esi] ; 1
mov esi,ptr3
mov eax,[esi] ; 4
exit
main ENDP
END main

----------------------------------

TITLE Summing an Array (SumArray.asm)
; This program sums an array of words.
; Last update: 2/1/02
INCLUDE Irvine32.inc
.data
intarray WORD 100h,200h,300h,400h
.code
main PROC
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[edi] ; add an integer
add edi,TYPE intarray ; point to next integer
loop L1 ; repeat until ECX = 0
exit
main ENDP
END main
-----------------------

يتبع >>>

----------


## شذى البنفسج

(RevStr.asm)
; This program reverses a string.
 INCLUDE Irvine32.inc
.data
aName BYTE "Abraham Lincoln",0
nameSize = ($ - aName) - 1
.code
main PROC
; Push the name on the stack.
 mov ecx,nameSize
 mov esi,0
L1: movzx eax,aName[esi] ; get character
 push eax ; push on stack
 inc esi
 Loop L1
; Pop the name from the stack, in reverse,
; and store in the aName array.
 mov ecx,nameSize
 mov esi,0
L2: pop eax ; get character
 mov aName[esi],al ; store in string
 inc esi
 Loop L2
; Display the name.
 mov edx,OFFSET aName
 call Writestring
 call Crlf
 exit
main ENDP
END main

-------------------------

     (Stack.asm)
; Testing PUSH and POP, 16-bit and 32-bit operands

Include Irvine32.inc
.code
main PROC
 call DumpRegs
 push 1
 call DumpRegs
 push eax
 call DumpRegs
 push eax
 call DumpRegs
 exit
main ENDP
END main

-------------------------------------

TITLE Integer Summation Program    (Sum1.asm)
; This program inputs multiple integers from the user,
; stores them in an array, calculates the sum of the
; array, and displays the sum. Chapter 5 example.
INCLUDE Irvine32.inc
.data
first DWORD 2323423424
second BYTE "adjaslfdjsl"
.code
main PROC
; Main program control procedure.
; Calls: Clrscr, PromptForIntegers,
;        ArraySum, DisplaySum
 exit
main ENDP

PromptForIntegers PROC
;
; Prompts the user for an array of integers, and
; fills the array with the user's input.
; Receives: ESI points to an array of
;   doubleword integers, ECX = array size.
; Returns: the array contains the values
;   entered by the user
; Calls: ReadInt, WriteString

 ret
PromptForIntegers ENDP

ArraySum PROC
;
; Calculates the sum of an array of 32-bit integers.
; Receives: ESI points to the array, ECX = array size
; Returns:  EAX = sum of the array elements

 ret
ArraySum ENDP

DisplaySum PROC
;
; Displays the sum on the screen
; Recevies: EAX = the sum
; Calls: WriteString, WriteInt

 ret
DisplaySum ENDP
END main

-----------------------------------

TITLE Integer Summation Program        (Sum2.asm)
; This program inputs multiple integers from the user,
; stores them in an array, calculates the sum of the
; array, and displays the sum.
INCLUDE Irvine32.inc
IntegerCount = 3   ; array size
.data
prompt1 BYTE  "Enter a signed integer: ",0
prompt2 BYTE  "The sum of the integers is: ",0
array   DWORD  IntegerCount DUP(?)
.code
main PROC
 call Clrscr
 mov  esi,OFFSET array
 mov  ecx,IntegerCount
 call PromptForIntegers
 call ArraySum
 call DisplaySum
 exit
main ENDP

PromptForIntegers PROC
;
; Prompts the user for an array of integers, and fills
; the array with the user's input.
; Receives: ESI points to the array, ECX = array size
; Returns:  nothing

 pushad  ; save all registers
 mov  edx,OFFSET prompt1  ; address of the prompt
 cmp  ecx,0  ; array size <= 0?
 jle  L2  ; yes: quit
L1:
 call WriteString  ; display string
 call ReadInt  ; read integer into EAX
 call Crlf  ; go to next output line
 mov  [esi],eax  ; store in array
 add  esi,4  ; next integer
 loop L1
L2:
 popad  ; restore all registers
 ret
PromptForIntegers ENDP

ArraySum PROC
;
; Calculates the sum of an array of 32-bit integers.
; Receives: ESI points to the array, ECX = array size
; Returns:  EAX = sum of the array elements

 push  esi  ; save ESI, ECX
 push  ecx
 mov   eax,0  ; set the sum to zero
L1:
 add   eax,[esi]  ; add each integer to sum
 add   esi,4  ; point to next integer
 loop  L1  ; repeat for array size
L2:
 pop   ecx  ; restore ECX, ESI
 pop   esi
 ret  ; sum is in EAX
ArraySum ENDP

DisplaySum PROC
;
; Displays the sum on the screen
; Recevies: EAX = the sum
; Returns:  nothing

 push edx
 mov  edx,OFFSET prompt2  ; display message
 call WriteString
 call WriteInt  ; display EAX
 call Crlf
 pop  edx
 ret
DisplaySum ENDP
END main

----------


## شذى البنفسج

TITLE Testing the Link Library          (TestLib.asm)
; Testing the Irvine32 Library.
; Last update: 09/8/01
INCLUDE Irvine32.inc
CR = 0Dh  ; carriage return
LF = 0Ah  ; line feed
.data
rand1   BYTE "Generating 20 random integers between "
        BYTE "0 and 990:",CR,LF,0
rand2   BYTE "Generating 20 random integers between "
        BYTE "0 and FFFFFFFFh:",CR,LF,0
prompt1 BYTE "Press a key: ",0
prompt2 BYTE "Enter a 32-bit signed integer: ",0
prompt3 BYTE "Enter your name: ",0
msg1    BYTE "The following key was pressed: ",0
regs    BYTE "Displaying the registers:",CR,LF,0
hello   BYTE "Hello, ",0
buffer  BYTE 50 dup(0)
dwordVal DWORD ?
.code
main PROC
; Set text color to black text on white background:
 mov  eax,black + (white * 16)
 call SetTextColor
 call Clrscr  ; clear the screen
 call Randomize  ; reset random number sequence
; Generate 20 random integers between 0 and 999.
; Put a delay between each.
 mov  edx,OFFSET rand1  ; display message
 call WriteString
 mov  ecx,20  ; loop counter
 mov  dh,2  ; screen row 2
 mov  dl,0  ; screen column 2
L1: call Gotoxy
 mov  eax,1000  ; indicate top of range + 1
 call RandomRange  ; EAX = random integer
 call WriteDec  ; display in unsigned decimal
 mov  eax,500
 call Delay  ; pause for 500 milliseconds
 inc  dh  ; next screen row
 add  dl,2  ; move 2 columns to the right
 Loop L1
 call Crlf  ; new line
 call WaitMsg  ; "Press [Enter]..."
 call Clrscr  ; clear screen
; Input a signed decimal integer and redisplay it in
; various formats:
 mov  edx,OFFSET prompt2  ; "Enter a 32-bit..."
 call WriteString
 call ReadInt  ; input the integer
 mov  dwordVal,eax  ; save in a variable
 call Crlf  ; new line
 call WriteInt  ; display in signed decimal
 call Crlf
 call WriteHex  ; display in hexadecimal
 call Crlf
 call WriteBin  ; display in binary
 call Crlf
; Display the registers:
 call Crlf
 mov  edx,OFFSET regs  ; "Displaying the registers:"
 call WriteString
 call DumpRegs  ; display registers and flags
 call Crlf
; Display memory:
 mov  esi,OFFSET dwordVal  ; starting OFFSET
 mov  ecx,LENGTHOF dwordVal  ; number of units in dwordVal
 mov  ebx,TYPE dwordVal  ; size of a doubleword
 call DumpMem  ; display memory
 call Crlf  ; new line
 call WaitMsg  ; "Press [Enter].."
; Ask the user to input their name:
 call Clrscr  ; clear screen
 mov  edx,OFFSET prompt3  ; "Enter your name: "
 call WriteString
 mov  edx,OFFSET buffer  ; point to the buffer
 mov  ecx,SIZEOF buffer - 1  ; max. number characters
 call ReadString  ; input the name
 mov  edx,OFFSET hello  ; "Hello, "
 call WriteString
 mov  edx,OFFSET buffer  ; display the name
 call WriteString
 call Crlf
 exit
main ENDP
END main

----------


## شذى البنفسج

TITLE  Compound IF Statements         (Regist.asm)
; Simple college registration example that uses
; the .IF, .ENDIF, and .ELSEIF directives.
; Last update: 1/28/02
INCLUDE Irvine32.inc
.data
TRUE = 1
FALSE = 0
gradeAverage  WORD ?
credits       WORD ?
OkToRegister  BYTE ?
.code
main PROC
 mov gradeAverage,300
 mov credits,14
 call CheckRegistration
 exit
main ENDP
CheckRegistration PROC
 mov OkToRegister,FALSE
 .IF gradeAverage > 350
    mov OkToRegister,TRUE
 .ELSEIF (gradeAverage > 250) && (credits <= 16)
    mov OkToRegister,TRUE
 .ELSEIF (credits <= 12)
    mov OkToRegister,TRUE
 .ENDIF
 ret
CheckRegistration ENDP
END main

----------


## شذى البنفسج

TITLE  Set Cursor Example         (SetCur.asm)
; Use the .IF and .ENDIF directives to perform
; run-time range checks on parameters passed to
; the SetCursorPosition procedure.
; Last update: 1/28/02
INCLUDE Irvine32.inc
.code
main PROC
 mov dl,79 ; X-coordinate
 mov dh,24 ; Y-coordinate
 call SetCursorPosition
 exit
main ENDP

SetCursorPosition PROC
; Set the cursor position.
; Receives: DL = X-coordinate, DH = Y-coordinate
; Checks the ranges of DL and DH.
;------------------------------------------------
.data
BadXCoordMsg BYTE "X-Coordinate out of range!",0Dh,0Ah,0
BadYCoordMsg BYTE "Y-Coordinate out of range!",0Dh,0Ah,0
.code
 .IF (DL < 0) || (DL > 79)
    mov  edx,OFFSET BadXCoordMsg
    call WriteString
    jmp  quit
 .ENDIF
 .IF (DH < 0) || (DH > 24)
    mov  edx,OFFSET BadYCoordMsg
    call WriteString
    jmp  quit
 .ENDIF
 call Gotoxy
quit:
 ret
SetCursorPosition ENDP
END main

----------


## شذى البنفسج

TITLE Extended Addition Example           (ExtAdd.asm)
; This program calculates the sum of two 64-bit integers.
; Chapter 7 example.
; Last update: 07/18/01
INCLUDE Irvine16.inc
.data
op1 QWORD 0A2B2A40674981234h
op2 QWORD 08010870000234502h
sum DWORD 3 dup(?) ; = 0000000122C32B0674BB5736
.code
main PROC
 mov ax,@data
 mov ds,ax
 mov  esi,OFFSET op1  ; first operand
 mov  edi,OFFSET op2  ; second operand
 mov  ebx,OFFSET sum  ; sum operand
 mov  ecx,2             ; number of doublewords
 call Extended_Add
 mov  esi,OFFSET sum  ; dump memory
 mov  ebx,4
 mov  ecx,3
 call DumpMem
 exit
main ENDP
;--------------------------------------------------------
Extended_Add PROC
;
; Calculates the sum of two extended integers that are
;   stored as an array of doublewords.
; Receives: ESI and EDI point to the two integers,
; EBX points to a variable that will hold the sum, and
; ECX indicates the number of doublewords to be added.
;--------------------------------------------------------
 pushad
 clc                  ; clear the Carry flag
L1: mov eax,[esi]        ; get the first integer
 adc eax,[edi]        ; add the second integer
 pushfd                ; save the Carry flag
 mov [ebx],eax        ; store partial sum
 add esi,4           ; advance all 3 pointers
 add edi,4
 add ebx,4
 popfd                 ; restore the Carry flag
 loop L1             ; repeat the loop
 adc word ptr [ebx],0  ; add any leftover carry
 popad
 ret
Extended_Add ENDP
END main

----------


## شذى البنفسج

TITLE Multiplication Examples             (Multiply.asm)
; Exmples of both signed and unsigned multiplication.
INCLUDE Irvine16.inc
.code
main PROC
 mov ax,@data
 mov ds,ax
mov ax,255
mov bx,255
imul bx
;Example 1
mov al,5h
mov bl,10h
mul bl             ; CF = 0
;Example 2
.data
val1  WORD  2000h
val2  WORD  0100h
.code
mov ax,val1
mul val2  ; CF = 1
;Example 3:
mov eax,12345h
mov ebx,1000h
mul ebx  ; CF = 1
; IMUL Examples:
; Example 4:
.data
val3 SDWORD ?
.code
mov   eax,+4823424
mov   ebx,-423
imul  ebx ; EDX=FFFFFFFFh, EAX=86635D80h
mov val3,eax

 exit
main ENDP
END main

----------


## شذى البنفسج

TITLE Multiple Doubleword Shift            (MultiShift.asm)
; Demonstration of multi-doubleword shift, using
; SHR and RCR instructions.
; Last update: 7/22/01
INCLUDE Irvine16.inc
.data
ArraySize = 3
array DWORD ArraySize dup(99999999h) ; 1001 pattern...
.code
main PROC
 mov ax,@data
 mov ds,ax
 call ClrScr
 call DisplayArray  ; display the array
; Shift the doublewords 1 bit to the right:
 mov esi,0
 shr array[esi+8],1       ; highest dword
   rcr array[esi+4],1       ; middle dword, include Carry flag
   rcr array[esi],1       ; low dword, include Carry flag
 call DisplayArray  ; display the array
 exit
main ENDP
;----------------------------------------------------
DisplayArray PROC
;----------------------------------------------------
 pushad
 mov ecx,ArraySize
 mov esi,8
L1:
 mov  eax,array[esi]
 call WriteBin ; display binary bits
 sub  esi,4
 Loop L1
 call Crlf
 popad
 ret
DisplayArray ENDP
END main

----------


## saso 20

السلام عليكم
اخت شذى يسلمو كتير شنوه الامثله اجت بوقتها عطلة مع ال ميكرو يعنى انشاءالله بنصير احنا والميكرو موجب*موجب   :SnipeR (101): وليس سالب*موجب :SnipeR (40): 
بكرر شكري الك يا احلى ورده  :SnipeR (69):  :Eh S(7):

----------


## شذى البنفسج

> السلام عليكم
> اخت شذى يسلمو كتير شنوه الامثله اجت بوقتها عطلة مع ال ميكرو يعنى انشاءالله بنصير احنا والميكرو موجب*موجب وليس سالب*موجب
> بكرر شكري الك يا احلى ورده


 
لا شكرعلى واجب .. وان شاء الله تستفيدو من الامثلة .. نورتي..
 :Smile:

----------


## جسر الحياة

يسلمو إيديكي شذى

----------


## شذى الياسمين

يسلمو شذى ..
انا منزله الماده الفصل هاد بس لسا مو وصلنا نكتب برامج ..
بس اكيد حستفيد منهم الايام الجايه ..
يعطيكي العافيه ..

----------


## شذى البنفسج

> يسلمو إيديكي شذى


 
هلا عمر .. 

نورت ..

----------


## شذى البنفسج

> يسلمو شذى ..
> انا منزله الماده الفصل هاد بس لسا مو وصلنا نكتب برامج ..
> بس اكيد حستفيد منهم الايام الجايه ..
> يعطيكي العافيه ..


اهلا فيكي شذى .. ان شاء الله بتسفيدي منهم ..
 :Smile:

----------

