The Assembly Language Level Assembly Language Work with
The Assembly Language Level
Assembly Language § Work with symbols not low level representation § Each statement translates to one machine instruction l Programmer has access to all features and instructions available in machine language § Assembly language program can only run on one family of machines § General format: [label] Opcode Operand[operand…. . ] [ Comment] Square brackets indicate optional content When more than one operand is to be specified then commas are used as separators
Assembly Language § Label l provides symbolic names for memory address l Labels are referred to in branch statements § Operand: specifies § addresses as symbols, X, Y instead of 0010 and 0011 § registers by their names, R 1 instead of 01 § Opcode : specifies the operation to be performed § As symbols: Add instead of 011 § Comment § In text to clarify the meaning of the instruction/program
An Assembly Language Program N = I + J on Pentium II
An Assembly Language program contains three kinds of statements § Imperative statements Action to be performed during execution of the assembly program: Add, Mov Converted to m/c language § Declarative statements declares constants or storage area in a program: DW Not converted § Assembler directive statements ·direct the assembler to take certain action during assembly of the program. START, END statement Not converted into m/c language
Assembly lang. Stmt § § § AGAIN § Imperative § § § Declarative § § A § ONE START 100 READ --100) +0 9 114 A LOAD ----ADD LOOP ---DS DC END corresponding m/c level stmt. ONE ----ONE 4, AGAIN ---1 ‘ 1’ Assembler directives 114) --- START says first word of the object program should be placed in the machine location with address ‘ 100’ END no more assembly language statements remain to be processed.
Example Use the operations as follows and write a program to compute M= (I+J) * (K+L). The program is loaded at address 1000. LAC ADD STA MUL A A START LAC ADD STA LAC ADD MUL END 1000 I J INTER K L INTER Load the accumulator from A Add contents of A to accumulator Store contents of accumulator in A Multiply accumulator contents with contents of A
Macros § A macro is a group of assembler statements that realize a composite operation § The composite operation, say +, is used several times in the program: (A+B)*(C+D) § A macro is declared in two parts l Macro definition: gives a name to the new operation and specifies operands, if any l Macro Body: the assembly program that realizes the composite operation § After it is declared, the new operation can be repeatedly used in the program § The effect is to replace the name by the macro body (macro expansion occurs)
Macro Example Assembly language code for interchanging the same numbers appears twice Without a macro With macro named SWAP
Macro With Parameters Assembly language code for interchanging different numbers Parameters Without a macro With macro
Example Write a macro for adding two numbers and use it to compute M= (I+J) * (K+L). The program is loaded at address 1000. START 1000 ADDTWO MACRO LAC ADD STA MEND A, B, C A B C ADDTWO I, J, PART 1 ADDTWO K, L, PART 2 LAC PART 1 MUL PART 2 END keep result in PART 1 keep result in PART 2
Subroutines/Procedures A subroutine is a group of assembler statements that realize a composite operation The composite operation, say +, is used several times in the program: (A+B)*(C+D) A procedure is declared in two parts Definition: gives a name to the new operation and specifies operands, if any Body: the program that realizes the composite operation After it is declared, the new operation can be repeatedly used in the program The effect is to CALL the subroutine without EXPANDING it.
Before Call After Call The Jump to Subroutine Local variables of calling program C B A Return address Variables of F Top of Stack Call is done by a special JSR instruction
Subroutines/Procedures Actions for subroutine calls Values of local variables of callee must be saved return address save return address make available actual parameters to called sub routine make available return value to callee transfer control to callee at point of call Calls may be nested/recursive Need a stack to save and make available values
Subroutine CHANGE P Q Local PROC P 1, P 2 MOV EAX, P 1 MOV EBX, P 2 MOV P 2, EAX MOV P 1, EBX RETURN PUSH JSR P Q CHANGE call by value of P and Q PUSH JSR R S CHANGE call by value of R and S DEC DEC 4 5 29 a local variable of program
Subroutine CHANGE P Q local PROC P 1, P 2 MOV EAX, P 1 MOV EBX, P 2 MOV P 2, EAX MOV P 1, EBX RETURN PUSH JSR $P $Q CHANGE call by reference of P and Q PUSH JSR $R $S CHANGE call by reference of R and S DEC DEC 4 5 29 local variable of program
Macro vs. Procedure Call Question Macro Procedure When is the call made? During assembly During Execution Body inserted at point of call Yes No Procedure call inserted No Yes Return Instruction needed? No Yes Number of copies in the program One per call One only
Extern How to make different programmers collaborate Each writes different programs but may use the same symbols for labels and operands They want to share the names of procedures or exchange data The EXTERN assembler directive declares symbols as externally available PUBLIC allows others to access at the address specified Public Global Extern main CHANGE Read number 1 Read number 2 Change number 1, number 2 CHANGE PROC P 1, P 2 MOV EAX, P 1 MOV EBX, P 2 MOV P 2, EAX MOV P 1, EBX RETURN
Separately Assembled Programs Program 1: Uses extern Program 1 and Program 2 have their own address spaces Before executing, they must be combined into one single address space Program 2: Uses Public
- Slides: 19