[ASM] Tinh tong mang 1 chieu

  • Published on
    08-Apr-2018

  • View
    218

  • Download
    0

Embed Size (px)

Transcript

  • 8/7/2019 [ASM] Tinh tong mang 1 chieu

    1/2

    F:\My ASM CODE\[ASM] Tinh tong mang 1 chieu.asm Thursday, March 31, 2011 1:36 PM

    1 # Tinh tong mang 1 chieu

    2 # Author: huahongquan2007

    3 .data

    4 insert:

    5 .space 160 # tao khong gian cho 40 phan tu mang

    6 prompt:

    7 .ascii "Hay nhap cac phan tu cua mang can tinh\n"

    8 .asciiz "De ket thuc qua trinh nhap hay nhap so 0 "

    9 output:

    10 .asciiz "Tong cac phan tu la : \n"

    11 .text

    12 main:

    13 la $a0,prompt # Xuat cau prompt

    14 add $v0,$0,4 # PRINT STRING

    15 syscall

    16

    17 la $a0,insert # Load address insert vao $a0

    18 addi $t1,$t1,40 # bien COUNT

    19 Nhap:

    20 add $v0,$0,5 # READ INT

    21 syscall

    22 sw $v0,0($a0) # Luu gia tri vua nhap vao vi tri $a0 tro toi

    23 addi $a0,$a0,4 # Tang $a0 len 4

    24 addi $t1,$t1,-1 # Giam bien COUNT di 1

    25 beqz $v0,exit # Neu nhap vao 0 thi ket thuc nhap

    26 bnez $t1,Nhap # Neu COUNT != 0 thi lap lai Nhap

    27 exit:

    28 la $a0,insert # Load address insert vao $a0

    29 addi $a1,$a1,40

    30 sub $a1,$a1,$t1 # count

    31 jal ting_tong

    32 addi $s0,$v0,0 # Luu gia tri tong vao $s0

    33

    34 la $a0,output # Xuat cau output

    35 add $v0,$0,4 # PRINT STRING

    36 syscall

    37 addi $a0,$s0,0

    38 addi $v0,$0,1 # PRINT INT

    39 syscall

    40

    41 addi $v0,$0,10 # exit

    42 syscall

    43 #---------------------------------------------

    44 # Ham tinh tong

    45 # Su dung bien v0 de luu gia tri tong

    46 # Sau moi lan lap se giam $a1 xuong 1 don vi

    47 # Neu $a1 = 0 thi ket thuc lap

    48 #---------------------------------------------

    49 ting_tong:

    50 addi $v0,$0,0 # SUM = 0

    51 vong_lap:

    52 beqz $a1,ket_thuc # Neu count = 0 thi ket_thuc

    53 lw $t0,0($a0) # Load gia tri vao #t0

    54 add $v0,$v0,$t0 # v0 = v0 + t0

    55 addi $a0,$a0,4 # tang a0 len 4

    56 addi $a1,$a1,-1 # giam count xuong 1

    57 j vong_lap

    58

    -1-

  • 8/7/2019 [ASM] Tinh tong mang 1 chieu

    2/2

    F:\My ASM CODE\[ASM] Tinh tong mang 1 chieu.asm Thursday, March 31, 2011 1:36 PM

    59 ket_thuc:

    60 jr $ra

    -2-