Skip to main content

Program 10: Matrix Multiplication

    

**Program 10: Matrix Multiplication**


```assembly

.model small

.stack 100h


.data

    matrix1 dw 1, 2, 3, 4

    matrix2 dw 5, 6, 7, 8

    result dw 0, 0, 0, 0


.code

    main proc

        mov ax, 0              ; Initialize AX to 0

        

    outer_loop:

        mov cx, 2              ; Set loop counter for rows

        

    inner_loop:

        mov bx, ax             ; Copy AX to BX (row index)

        add bx, cx             ; Add CX to BX (row index + column index)

        

        mov dx, 0              ; Initialize DX (column index)

        

    multiply_loop:

        mov si, dx             ; Copy DX to SI (column index)

        add si, cx             ; Add CX to SI (row index + column index)

        

        mov ax, [matrix1 + bx * 2] ; Load element from matrix1

        mov bx, [matrix2 + si * 2] ; Load element from matrix2

        imul bx                 ; Multiply and store result in AX

        

        add [result + dx * 2], ax ; Add result to the appropriate location in the result matrix

        

        inc dx                  ; Increment column index

        cmp dx, 2              ; Compare with 2 (matrix dimension)

        jl multiply_loop       ; Jump if less than 2

        

        inc bx                  ; Increment row index

        loop inner_loop         ; Decrement CX and loop if not zero

        

        inc ax                  ; Increment AX (column index)

        cmp ax, 2              ; Compare with 2 (matrix dimension)

        jl outer_loop          ; Jump if less than 2

        

        ; Print result matrix

        mov ax, 0              ; Initialize AX (row index)

        mov cx, 2              ; Set loop counter for rows

        

    print_loop:

        mov dx, 0              ; Initialize DX (column index)

        

    print_inner_loop:

        mov bx, ax             ; Copy AX to BX (row index)

        add bx, dx             ; Add DX to BX (row index + column index)

        

        mov ax, [result + bx * 2] ; Load element from result matrix

        call print_number

        

        inc dx                  ; Increment column index

        cmp dx, 2              ; Compare with 2 (matrix dimension)

        jl print_inner_loop    ; Jump if less than 2

        

        call print_newline      ; Print newline

        

        inc ax                  ; Increment AX (row index)

        loop print_loop         ; Decrement CX and loop if not zero

        

        mov ah, 4Ch             ; Exit program

        int 21h

    main endp


print_number proc

    push bx

    push cx

    

    mov bx, ax

    mov cx, 10

    

    mov ah, 0               ; Initialize quotient (AH) to 0

    mov dx, 0               ; Initialize remainder (DX) to 0

    

    reverse_loop:

        div cx               ; Divide BX by 10, result in AX, remainder in DX

        add dl, 30h          ; Convert remainder to ASCII

        push dx              ; Push ASCII character onto stack

        mov dx, 0            ; Clear DX for next iteration

        

        test ax, ax          ; Check if quotient is 0

        jnz reverse_loop     ; If not zero, loop again

    

    print_loop:

        pop dx               ; Pop ASCII character from stack

        mov ah, 02h          ; DOS function to print character

        int 21h              ; Print character

        

        loop print_loop      ; Loop until characters are printed

    

    pop cx

    pop bx

    ret

print_number endp


print_newline proc

    mov dl, 0Ah             ; ASCII code for newline

    mov ah, 02h             ; DOS function to print character

    int 21h

    ret

print_newline endp

end main

```


Explanation:

1. `.model small` and `.stack 100h`: Memory model and stack size definitions.

2. `.data` section: Declares the data segment.

   - `matrix1 dw 1, 2, 3, 4`: Defines a 2x2 matrix named "matrix1".

   - `matrix2 dw 5, 6, 7, 8`: Defines a 2x2 matrix named "matrix2".

   - `result dw 0, 0, 0, 0`: Defines a 2x2 matrix named "result" for storing the multiplication result.

3. `.code` section: Contains the main code.

   - `mov ax, 0`: Initialize AX to 0 (row index).

   - `outer_loop:`: Label for the outer loop (rows).

   - `mov cx, 2`: Set CX to 2 (loop counter for rows).

   - `inner_loop:`: Label for the inner loop (columns).

   - `mov bx, ax`: Copy AX to BX (row index).

   - `add bx, cx`: Add CX to BX (row index + column index).

   - `mov dx, 0`: Initialize DX (column index).

   - `multiply_loop:`: Label for the loop to perform multiplication and summation.

   - `mov si, dx`: Copy DX to SI (column index).

   - `add si, cx`: Add CX to SI (row index + column index).

   - `mov ax, [matrix1 + bx * 2]`: Load element from "matrix1".

   - `mov bx, [matrix2 + si * 2]`: Load element from "matrix2".

   - `imul bx`: Multiply and store result in AX.

   - `add [result + dx * 2], ax`: Add result to the appropriate location in "result" matrix.

   - `inc dx`: Increment column index.

   - `cmp dx, 2`: Compare with 2 (matrix dimension).

   - `jl multiply_loop`: Jump if less than 2 (loop to multiply next element).

   - `inc bx`: Increment row index.

   - `loop inner_loop`: Decrement CX and loop if not zero.

   - `inc ax`: Increment AX (column index).

   - `cmp ax, 2`: Compare with 2 (matrix dimension).

   - `jl outer_loop`: Jump if less than 2 (loop to multiply next row).

   - `print_loop:`: Label for the loop to print the result matrix.

   - `mov ax, 0`: Initialize AX (row index).

   - `mov cx, 2`: Set CX to 2 (loop counter for rows).

   - `print_inner_loop:`: Label for the inner loop to print matrix elements.

   - `mov bx, ax`: Copy AX to BX (row index).

   - `add bx, dx`: Add DX to BX (row index + column index).

   - `mov ax, [result + bx * 2]`: Load element from "result" matrix.

   - `call print_number`: Call procedure to print the number.

   - `inc dx`: Increment column index.

   - `cmp dx, 2`: Compare with 2 (matrix dimension).

   - `jl print_inner_loop`: Jump if less than 2 (loop to print next column).

   - `call print_newline`: Print newline to separate rows.

   - `inc ax`: Increment AX (row index).

   - `loop print_loop`: Decrement CX and loop if not zero.

   - `mov ah, 4Ch`: Set the exit code for DOS.

   - `int 21h`: Call DOS interrupt to exit the program.

   

The above programs showcase a range of assembly language concepts and techniques, including basic arithmetic, loops, conditional jumps, printing, and procedures. Keep in mind that these are relatively simple examples for demonstration purposes, and real-world assembly programs may involve more complexity and optimizations.


Comments

Popular posts from this blog

6. Generate a YACC specification to recognize a valid arithmetic expression that uses operators +, – , *,/ and parenthesis.

Below is a YACC specification for recognizing valid arithmetic expressions that involve operators (+, -, *, /) and parentheses. I'll explain each line of the code afterward: ```yacc %{ #include <stdio.h> %} %token NUMBER %left '+' '-' %left '*' '/' %% expression: expression '+' expression            | expression '-' expression            | expression '*' expression            | expression '/' expression            | '(' expression ')'            | NUMBER            ; %% int yylex() {     // Code for tokenizing input and returning tokens } void yyerror(const char *s) {     fprintf(stderr, "Error: %s\n", s); } int main() {     yyparse();     return 0; } ``` Now, let's break down the YACC specification line by line: 1. `%{` ... `%}`: This is the C code section where y...

1, Design and implement a lexical analyzer using C language to recognize all valid tokens in the input program. The lexical analyzer should ignore redundant spaces, tabs and newlines. It should also ignore comments

#include <stdio.h> #include <ctype.h> int main() {     char c;     while ((c = getchar()) != EOF) {         if (isspace(c)) {             // Ignore whitespace, tabs, and newlines             continue;         }         if (c == '/') {             // Check for comments             char nextChar = getchar();             if (nextChar == '/') {                 while ((nextChar = getchar()) != '\n');         ...

17. Write a program to perform constant propagation.

Sure, I can provide you with a basic example of a constant propagation program along with explanations for each line. Please note that this example is simplified and might not cover all possible cases of constant propagation. ```c #include <stdio.h> #include <stdbool.h> // Structure to represent an assignment statement struct Assignment {     char variable;     int value;     bool isConstant; }; int main() {     // Example assignments (variable, value, isConstant)     struct Assignment assignments[] = {         {'a', 10, true},         {'b', 20, true},         {'c', 0, true},         {'d', 30, false},         {'e', 0, false}     };     int numAssignments = sizeof(assignments) / sizeof(assignments[0]);     // Perform constant propagation     for (int i = 0; i < numAssignments; i++) { ...