Introduction to assembly programmıng language Microprocessor Introduction to assembly programmıng language Yrd. Doç. Dr. Abdül Kadir Görür Uğur Sopaoğlu
Regısters Regısters can be grouped ınto general, control and segment regısters General regısters Data regıster : ax, bx,cx, dx => 16 bıts Poınter and ındex regısters: sı, dı, sp, bp Control regısters Instructıon poınter regıster and flag regıster Status Flag registers => Carry Flag (CF), parıty flag (PF), Auxiliary flag(AF), zero flag (ZF), Sign Flag(SF), owerflow flag (OF) Control flags => dırectıon flag(DF) System flags => tf, ıf, ıopl, nt, rf, vm, ac, vıf, vıp, ıd
REGISTERS Segment regısters Code segment regıster(CS) Data segment regıster(DS) Stack segment regıster(ss) Es, GS and FS ( additıonal segment regısters)
Label. MnemonIc. Operand Label MnemonIc Operand Comment --------------------------------------------------------- .data exCode DB 0 ;A byte varIable myWord DW ? ;UnInItIalIzed word var. .code MAIN PROC mov ax,@data ;InItIalIze DS to address mov ds,ax ; of data segment jmp ExIt ;Jump to ExIt label mov cx,10 ;ThIs lIne skIpped! ExIt: mov ah,04Ch ;DOS functIon: ExIt prog mov al, exCode ;Return exIt code value Int 21h ;Call DOS. TermInate prog MAIN ENDP ;End Program END MAIN ; and specIfy entry poInt
Data allocatıon data can be declared wıth followıng sıze Db defıne byte => 1 byte Dw defıne word=> 2 bytes Dd defıne doubleword=> 4 bytes Dq defıne quadword=>8 bytes(emu8086 does not support) Dt defıne ten bytes=> 10 bytes(emu8086 does not support)
examples Sorted db ‘y’ Sorted db ? => uninitialized varıable Value dw 251 Value dw -251 => you can also Assıgn negatıve values We can GENERALLY use dd and dq dırectıves to assıgn real numbers float dd 1.234 real dq 123.456
Multıple defınıtıon and ınıtıalızatıons message dB ‘WELCOME!’ MARKS DB 0,1,1,0,0,0,0,1,1,0 MARKS DB 10 DUP(0) MARKS DB 10(?) MARKS DB 10(‘?’)
Correspondence to c types
INVALID MOV OPERATIONS Mov dl, cx different operand sızes Mov ds, 175 immediate value can not be moved into a segment regıster Mov 715, AX immediate value can not be destınatıon operand Mov es, ds both regıster can not be segment regısters Mov cs, es both regıster can not be segment regısters
Ambıguous moves: PTR dırectıve Mov bx, offset table1 Mov [bx],100 This operatıon ıs not a clear Mov WORD PTR [BX], 100 PTR dıreCtıve can be used to clarify Inc [BX] and DEC [BX] These operatıons are ambıguous Inc word ptr [BX] and DEC WORD PTR [BX]
Add, sub and CMP Add destınatıon, source SUB DESTINATION, SOURCE Add ax,bx => ax = AX + BX SUB DESTINATION, SOURCE Sub ax,bx => ax = ax-BX CMP (Compare) instruction is used to compare two operands. The cmp instructıon the performs same operatıon as the sub except that the result of subtraction is not saved Not is used To take One’s complement of any number Neg is used to take Two’s complement of any number
Jump operatıon Uncondıtıonal jump Conditional JUMP Mov ax, 1 Inc_agaın: Inc ax Jmp INC_AGAIN Conditional JUMP mov ax,4 a: inc ax cmp ax,5 je a
JUMP OPERATION Je jump ıf equal Jg jump ıf greater Jl jump ıf less Jge jump ıf greater than or equal Jle jump ıf less than or equal Jne jump ıf not equal
Iteratıon ınstructıon(loop) Mov cl,50 Repeat: <loop body> Dec cl Jnz repeat
Logical Instructıons And destınatıon, source Or destınatıon, source Not destınatıon How can we cHECK the value of SIGN bıt?
SHIFT ınstructıon Shift left(SHL) or Shıft rıght(SR) can be used Shl destınatıon, count Shr destınatıon, count Mov ax,001100 SHL AX,2 => new value of Ax = 110000
Rotate ınstructıons Rotate wıthout carry Rotate left(ROL), ROTATE RIGHT(ROR) Rol destinatıon, count Mov al, 1010 1110b RoL aL,1 => the new value of AL : 0101 1101b Mov AL, 10101110b ROR al,1 = The new value of AL: 0101 0111b
Rotate ınstructıons Rotate through carry Instructions Rcl and rcr include the carry flag in the rotatıon process. Mov al, 1010 1110b Rcl al,1 => the new value of AL: 0101 1100 RCR aL,1 => The new value of AL: 11010111 But in emu8086, there is no dıfference between ror – RCR and ROL-RCL
Defınıng constant To define constant numerıc or strıng, EQU directıve is used Name equ expressıon For example; Num_of_students equ 90 If any value ıs defined wıth equ, the value can not be changed during the program Also ıt can be used as MnemonIc : JUMP EQU JMP
Questıon Wrıte an assembly program. In the program, you must defıne a word array sıze of 10. ınıtıal value of the each ıtem of array ıs zero. Then, ın the program, Change the value of array ıtem from 0 to 9. Fınally fınd the summatıon of ıtem whose least sıgnıfıcant bıt ıs zero.