rss
twitter
facebook

Home

Mostrar mensagens com a etiqueta Assembler - Mips. Mostrar todas as mensagens
Mostrar mensagens com a etiqueta Assembler - Mips. Mostrar todas as mensagens

String Reversed

   
# Colocar uma string com 13chars
# e Meter ao contrario de forma inversa



    .data
        .align 1
        String: .space 14 #*change
        msg1: .asciiz "Pls give a character: "
        msg2: .asciiz "\n"
        msg3: .asciiz "String is: "
        msg4: .asciiz "\nString Reversed is: "
    .text
    .globl main
main:

addi $s0,$zero,13 #*change
addi $t0,$zero,0

in:
    la $a0,msg2
    li $v0,4
    syscall

    li $v0,4
    la $a0,msg1
    syscall
    li $v0,12
    syscall

    add $t1,$v0,$zero
    sb $t1,String($t0)
    addi $t0,$t0,1
    slt $t1,$s0,$t0
    beq $t1,$zero,in

    sb $zero,String($t0) #ending zero

    la $a0,msg2
    li $v0,4
    syscall
    la $a0,msg2
    li $v0,4
    syscall
    la $a0,msg3
    li $v0,4
    syscall

    la $a0,String
    li $v0,4
    syscall

    addi $a1,$zero,14 #pass length-*change
    jal stringreverse #reverse

    la $a0,msg2
    li $v0,4
    syscall

    la $a0,msg4
    li $v0,4
    syscall

    la $a0,String
    li $v0,4
    syscall

    li $v0,10
    syscall


stringreverse:

            add $t0,$a0,$zero        #beginning address

            add $t1,$zero,$zero        #i=0
            addi $t2,$a1,-1            #j=length-1

loop:

            add $t3,$t0,$t1
            lb $t4,0($t3)            #lb String[i]

            add $t5,$t0,$t2
            lb $t6,0($t5)            #lb String[j]

            sb $t4,0($t5)            #String[j]=String[i]
            sb $t6,0($t3)            #String[i]=String[j]

            addi $t1,$t1,1            #i++
            addi $t2,$t2,-1            #j--

#if i>=j break - $t1<$t2
slt $t6,$t2,$t1
beqz $t6,loop

jr $ra

#i-0;j=length-1;
# do {
# x = str[i]
# str[i]=str[j]
# str[j] = x
# i++;j--;
# } while(!(j<i))
Read More

branch to target

        b    target        #  unconditional branch to program label target
        beq    $t0,$t1,target    #  branch to target if  $t0 = $t1
        blt    $t0,$t1,target    #  branch to target if  $t0 < $t1
        ble    $t0,$t1,target    #  branch to target if  $t0 <= $t1
        bgt    $t0,$t1,target    #  branch to target if  $t0 > $t1
        bge    $t0,$t1,target    #  branch to target if  $t0 >= $t1
        bne    $t0,$t1,target    #  branch to target if  $t0 <> $t1
Read More

calculation is recursive.

# This file can be used to write all the programs in PCSpim
# NAME:
# TA's NAME:
# DESCRIPTION:

## Example program in revised section 9.7 of Goodman and
## Miller's "A Programmer's View of Computer Architecture"
##

## This MAL program computes the greatest common divisor of
## two positive integers. The function that does the main
## calculation is recursive.

        .data
# data items, i.e. all initialized and
# non-initialized items go in here

err_msg:     .asciiz "\nbad integer entered\n"

        .text
        .globl main

main:       

# your main subroutine code goes in here

        jal intro
        jal getint
        beqz $v0, input_err
        move $s0, $v1
        jal getint
        beqz $v0, input_err
        move $a1, $v1
        move $a0, $s0
        jal gcd
        move $a0, $v0
        jal print_result
        done

input_err:    la $8, err_msg
        puts ($8)

# the last line of main
        done

# end of main
# any subroutines may be inserted here:...

## Intro
## A simple procedure to print a little introduction message.
## There are no parameters.

        .data
msg1:    .asciiz "This program computes the greatest common divisor of \n"
msg2:    .asciiz "two user entered integers.\n\n"

        .text
intro:        la $8, msg1
        puts ($8)
        la $8, msg2
        puts ($8)
        jr $ra

## getint
## A function to get a single positive integer from the user.
## return values:
##  $v0 -- flag indicating success of the function
##         value is 1 if integer entered is ok.
##         value is 0 if not ok.
##  $v1 -- the integer entered
##
## register assignments:
##  $8 -- user entered character
##  $9 -- digit of integer
##  $10 -- newline character (the constant)
##  $11 -- the integer being entered
##  $12 -- the constant 10, the base
##  $13 -- temp
##  $14 -- the constant 9

        .data
prompt:        .asciiz "Enter positive integer: "

        .text
getint:        li $10, 10     # $10 <- newline character
        li $12, 10    # constant 10
        li $11, 0
        li $14, 9
        la $13, prompt
        puts ($13)

getchar:    getc $8
        beq $8, $10, getint_rtn
        sub $9, $8, 48
        bgt $9, $14, int_err
        bltz $9, int_err
        mul $11, $11, $12
        add $11, $11, $9
        b getchar

getint_rtn:    li $v0, 1
        move $v1, $11
        jr $ra

int_err:    li $v0, 0
        jr $ra

##  gcd
##  A recursive function to calculate the greatest common divisor.
##
##    gcd(m, n) = m            if n = 0
##              = gcd (n, m mod n)     if n > 0
##  return values:
##    $v0 -- the greatest common divisor
##  parameters:
##    $a0 -- m
##    $a1 -- n
##

        .data
gcd_err_msg:    .asciiz "error in calculating gcd -- quitting.\n"

        .text

gcd:        sub $sp, $sp, 12    # allocate activation record
        sw $ra, 12($sp)        # save return address
        bgtz $a1, n_greater
        bnez $a1, gcd_err
        move $v0, $a0        # return m
        b gcd_rtn

n_greater:    sw $a0, 8($sp)        # save current parameters
        sw $a1, 4($sp)
        rem $8, $a0, $a1
        move $a0, $a1        # set up parameters for call
        move $a1, $8
        jal gcd            # recursive call to gcd
        lw $a0, 8($sp)        # restore current parameters
        lw $a1, 4($sp)
            # return value already in $v0

gcd_rtn:    lw $ra, 12($sp)        # restore return address
        add $sp, $sp, 12    # remove activation record
        jr $ra            # return

gcd_err:    la $8, gcd_err_msg
        puts ($8)
        done

## print_result
##  Procedure to print out the base ten integer passed to the procedure
##  as a parameter
##
## return values:
##  none
## parameters:
##  $a0 -- the integer to be printed out
##
## register assignments:
##  $8 -- address of output string
##  $9 -- stack pointer before pushing characters onto stack
##  $10 -- copy of integer to be printed
##  $11 -- ASCII character code of digit to be printed as pushed
##  $12 -- ASCII character code of digit to be printed as popped
##  $13 -- newline character for nice output

        .data
result_msg:    .asciiz "The greatest common divisor is "

        .text
print_result:    move $10, $a0
        la $8, result_msg
        puts ($8)
        move $9, $sp
        bnez $10, push_loop
        li $11, 0        # special case for printing 0
        sw $11, 0($sp)
        sub $sp, $sp, 4
        b print_loop

push_loop:    rem $11, $10, 10    # push characters onto stack
        add $11, $11, 48
        sw $11, 0($sp)
        sub $sp, $sp, 4
        div $10, $10, 10
        bnez $10, push_loop

print_loop:    add $sp, $sp, 4        # pop characters off stack
                    # and print

        lw $12, 0($sp)
        putc $12
        bne $9, $sp, print_loop
       
        li $13, 10         # print newline
        putc $13
        jr $ra

# END OF PROGRAM (leave this line here)
Read More

This program does simple vector addition

# NAME:
# TA's NAME:
# DESCRIPTION:

##     Example Program in section 9.7 (p 243) of "A Programmer's
##    View of Computer Architecture" of Goodman and Miller
##
##    This program does simple vector addition

    .data

# data items, i.e. all initialized and
# non-initialized items go in here

prompt1:    .asciiz "Vector addition program\n"
prompt2:    .asciiz "Please enter a vector\n"
linefeed:    .byte '\n'
err_msg:    .asciiz "Error encountered - quitting\n"

    .text
    .globl main

main:   
# your main subroutine code goes in here

    la $8, prompt1
    puts ($8)
    la $8, prompt2
    puts ($8)
    jal get_vector
    beq $2, $0, error1

# copy parameters out of registers
    move $16, $4        # $16 is x1
    move $17, $5        # $17 is y1
    move $18, $6        # $18 is z1

    la $8, prompt2
    puts ($8)
    jal get_vector
    beq $2, $0, error1

# copy parameters out of registers
    move $19, $4
    move $20, $5
    move $21, $6

# push parameters on stack for addition procedure

    add $sp, $sp, -24
    sw $16, 24($sp)
    sw $17, 20($sp)
    sw $18, 16($sp)
    sw $19, 12($sp)
    sw $20, 8($sp)
    sw $21, 4($sp)

    jal vector_add
    add $sp, $sp, 24    # pop parameters and discard

# the results x3, y3, z3 are already in $4, $5, and $6
# so don't move them

    jal print_result

# the last line of main
    done

# end of main
# any subroutines may be inserted here:...

error1:    la $8, err_msg
    puts ($8)
    done

# get_vector -- a procedure to get an x, y, and z for
#         a vector
# return values: $2 -- error flag, 0 if there is an error
#         $4 -- the x value
#         $5 -- the y value
#         $6 -- the z value

    .data
getx:    .asciiz "Enter x: "
gety:    .asciiz "Enter y: "
getz:    .asciiz "Enter z: "

    .text
get_vector:
    sw $31, 0($sp)    #  pushgm (Release 2.03) is a macro of original
    add $sp, $sp, -4    #  code in textbook

    la $8, getx    #  get x
    puts ($8)
    jal get_integer
    beq $2, $0, return1
    move $22, $3

    la $8, gety    #  get y
    puts ($8)
    jal get_integer
    beq $2, $0, return1
    move $23, $3

    la $8, getz    #  get z
    puts ($8)
    jal get_integer
    beq $2, $0, return1
    move $24, $3

return1:
    move $4, $22
    move $5, $23
    move $6, $24

    add $sp, $sp, 4
    lw $31, 0($sp)
    jr $31

# get_integer -- a procedure to form an integer from
#    a single line of user input
# return values:
#    $2 -- error flag, 0 if there is an error
#    $3 -- the integer value

    .data
    bias = 48

    .text
get_integer:
    sw $31, 0($sp)    # save return address
    add $sp, $sp, -4
    li $15, 1    # initialize variables
    li $3, 0
    lbu $11, linefeed
            # $11 -- the <CR> character

    li $9, bias    # $9 -- '0' ASCII character
            # code
            # NOTE: Not the same as original
            # using 'lw'

    li $13, '0'
    li $14, '9'
    getc $8        # $8 -- the user input character

get_digit:
    beq$8, $11, return2
            # integer is complete
            # when <CR> read
    blt $8, $13, bad_input
    bgt $8, $14, bad_input
    sub $10, $8, $9
    mul $3, $3, 10
    add $3, $3, $10
    getc $8
    b get_digit

bad_input:
    li $15, 0

return2:
    move $2, $15
    add $sp, $sp, 4    # restore return address
    lw $31, 0($sp)
    jr $31

# vector_add -- a procedure to add 2 vectors together
# return values:
#    $2 -- error flag, 0 if there is an error
#    $4 -- the x component
#    $5 -- the y component
#    $6 -- the z component
# parameters: on stack, x1, y1, z1, x2, y2, z2

vector_add:
    sw $31, 0($sp)    # save return address
    add $sp, $sp, -4

    lw $8, 28($sp)    # pop x components and add
    lw $9, 16($sp)
    add $4, $8, $9

    lw $8, 24($sp)    # pop y components and add
    lw $9, 12($sp)
    add $5, $8, $9

    lw $8, 20($sp)    # pop x components and add
    lw $9, 8($sp)
    add $6, $8, $9

return3:
    add $sp, $sp, 4    # restore return address
    lw $31, 0($sp)
    jr $31

# print_result -- a procedure to print out the 3 values
#    computed
# return values: none
# parameters:
#    $4 -- the x component to be printed
#    $5 -- the y component to be printed
#    $6 -- the z component to be printed

    .data
msg:    .asciiz "The result of the vector addition: \n"
x_msg:    .asciiz "x = "
y_msg:    .asciiz "y = "
z_msg:    .asciiz "z = "

    .text
print_result:
    sw $31, 0($sp)        # save return address
    add $sp, $sp, -4

    add $sp, $sp, -12    # save parameters, since
    sw $4, 12($sp)        # other procedures like
    sw $5, 8($sp)        # 'puts' also use $4 - $7
    sw $6, 4($sp)

    la $8, msg
    puts ($8)

    la $8, x_msg
    puts ($8)
    lw $4, 12($sp)        # x is on the stack
    jal print_int
    la $9, linefeed
    lbu $9, 0($9)
    putc $9

    la $8, y_msg
    puts ($8)
    lw $4, 8($sp)        # y is on the stack
    jal print_int
    la $9, linefeed
    lbu $9, 0($9)
    putc $9

    la $8, z_msg
    puts ($8)
    lw $4, 4($sp)        # z is on the stack
    jal print_int
    la $9, linefeed
    lbu $9, 0($9)
    putc $9

    lw $4, 12($sp)    # restore parameters
    lw $5, 8($sp)
    lw $6, 4($sp)
    add $sp, $sp, 12

return4:
    add $sp, $sp, 4    # restore return address
    lw $31, 0($sp)

    jr $31

# print_int -- a procedure to print out an integer
# return values: none
# parameters: $4 -- the integer to be printed

print_int:
    sw $31, 0($sp)    # save return address
    add $sp, $sp, -4

    li $10, 0    # $10 -- count of
            # number of digits

    move $12, $4
    sw $4, 0($sp)
    add $sp, $sp, -4

# repeat loop to peel off digits from the least
# significant end of the value
repeat:
    rem $9, $12, 10
    div $12, $12, 19
    add $9, $9, 48    # add back in the bias
    sw $9, 0($sp)    # push ASCII char. on stack
    add $sp, $sp, -4
    add $10, $10, 1
    bne $12, $0, repeat

# repeat loop to print out the characters stored on stack
print_em:
    add $sp, $sp, 4
    lw $11, 0($sp)
    putc $11
    sub $10, $10, 1
    bne $10, $0, print_em

    add $sp, $sp, 4
    lw $4, 0($sp)
return5:
    add $sp, $sp, 4
    lw $31, 0($sp)
    jr $31

# END OF PROGRAM (leave this line here)
Read More

This sample code illustrates basic I/O

# NAME:
# TA's NAME:
# DESCRIPTION:

#
#----------------------------------------------#
#
# This sample code illustrates basic I/O
# USING SYSCALLS to the console, along with a
# small amount of array manipulation.
#
#----------------------------------------------#

# This is the data segment

        .data       
string0:    .asciiz     "This is a text string\n"
item0:    .word        99
array0:    .word        11 22 33 44 55 66 77 88 99 #array of constants

#----------------------------------------------#

# This is the the code ("text") segment
    .text
        .globl main   

main:           

# EXAMPLE I:  Print a string on the console.  The method is to load the
# address of the string into $a0 and then use a syscall to print the string.

    la $a0, string0        # Load the base address of the string into $a0
    li $v0, 4        # Set $v0 to 4, this tells syscall to print the
                # text string specified by $a0
    syscall            # Now print the text string to the console

# EXAMPLE II: Load an integer from the data segment and print it.

    lw $a0, item0        # Load the value of item0 into $a0
    li $v0, 1        # Set $v0 to 1, this tells syscall to print the
                # integer specified by $a0   
    syscall            # Now print the integer

# EXAMPLE III: Read an integer from the console.

    li $v0, 5        # Set $v0 to 5, this tells syscall to read an
                # integer from the console
    syscall            # Now read the integer.
                #
                # The integer is now in $v0, and we can use
                # it as we see fit.

# EXAMPLE IV: Print an integer (the one that is the 4th element of an array)

        li  $t0, 3        # Set t0 to 3, this is the index of the
                # element we are fetching from the array
        li  $t1, 4        # Set t1 to 4, this is the size in bytes of an
                # element (this is an integer array)
        mul $t2, $t1, $t0    # t2 = t1 * t0, so t2 is the BYTE offset of the
                  # element we are fetching from the array.
    lw  $a0, array0($t2)    # Load the element of the array; note that
                # the first element in the array has an offset
                # of zero
    li  $v0, 1        # Set $v0 to 1, this tells syscall to print the
                # integer specified by $a0
    syscall            # Now print the integer

# EXAMPLE V:  Halt the program.

    li $v0, 10        # set $v0 to 10, this tells syscall to end
                # execution of this program
    syscall
   
Read More

Please enter a positive integer in hex

##   C++ version:
##   void factorial(int input, int output){
##     int n = 0;
##     int fact = 0;
##     int i = 0;
##
##     cout << "Please enter a positive integer: ";
##     cin >> hex >> n;
##     if (n > 0){
##       fact = 1;
##       while (i < n){
##         fact = fact * i;
##         i++;
##       }
##       cout << "The fact of ";
##       cout << hex << n << " is " << fact << endl;
##     }
##     else
##       cout << "Number negative WRONG !" << endl;
##   }




   .data

# data items, i.e. all initialized and
# non-initialized items go in here

#strings for making the output look nice

str1:   .asciiz "Please enter a positive integer in hex: "
str2:   .asciiz "The factorial of "
str3:   .asciiz " is in hex "
str4:   .asciiz "Number negative WRONG ! "
newline: .asciiz "\n"

##      Assume that the compiler chooses to use registers for n,fact,i:
##      $8 = n         (user entered character)
##      $9 = fact      (running fact of the first n integers)
##      $10 = i         (integer to be added into sum, runs from 0 to n)
##      $11 = tmp       (used for comparison of i and n)

        .text
        .globl main    # declaration of main as a global variable

main:   li $8, 0
        li $9, 1
        li $10, 1
        li $11, 0

        puts str1

# the following 3 lines read a hex number and store it in $8

        li $2, 500     ## replace "get n" of SAL code since MAL
        syscall         ## does not have "get"
        move $8, $2   ##

        puts newline

if :    sub $11, $8, $11  ## if n > 0
        bltz $11, else 
then:     
while:  sub $11, $8, $10
        bltz $11, endwhile
loop:   mul $9, $9, $10
        add $10, $10, 1
        b while

endwhile:  puts str2
       
        li $2, 600     ## replacement of "put n" of SAL code
        move $4, $8
        syscall

        puts str3
       
        li $2, 600     ## replace "put sum" of SAL code
        move $4, $9
        syscall

        puts newline
      
        b end_if

# here starts the else part

else:   puts str4
       
        li $2, 600     ## replace "put sum" of SAL code
        move $4, $9
        syscall

        puts newline
      
end_if:
# the last line of main

        done

# end of main
# any subroutines may be inserted here:...

# END OF PROGRAM (leave this line here)
Read More

power X^Y

.data
str1: .asciiz "Give integer X from 1 to 20 "
str2: .asciiz "Give integer Y from 0 to 5 "
errormsg: .asciiz "Out of range.\n"
nline: .asciiz "\n"
result1: .asciiz " raised to "
result2: .asciiz " gives: "
.text

error:
li $v0,4 #print string1
la $a0,errormsg
syscall
beq $s2,$zero,getX
j getY

.globl main
main:
addi $s0,$zero,21 #s0=21
addi $s1,$zero,6 #s1=6

getX:
addi $s2,$zero,0 #s2=0 to input x and 1 to input y
li $v0,4
la $a0,str1
syscall #print string1
li $v0,5
syscall #read int
slt $s3,$v0,$s0
beq $s3,$zero,error
blez $v0,error
move $t0,$v0

getY:
addi $s2,$zero,1
li $v0,4
la $a0,str2
syscall #print string2
li $v0,5
syscall #read int
slt $s3,$v0,$s1
beq $s3,$zero,error
bltz $v0,error

beq $t1,$zero,else #if (t1=0) t2=1, t1=y,t2=result
addi $t2,$zero,1
addi $s4,$zero,0

loop: #if s4slt $s5,$s4,$t1
beq $s5,$zero,printresult
#if $s1=0 goto printresult(s4=t1)
mul $t2,$t2,$t0 #t2=t2*t0
addi $s4,$s4,1
j loop

else:
addi $t2,$zero,1
j printresult

printresult:
li $v0,1
move $a0,$t0
syscall #print X
li $v0,4
la $a0,result1
syscall #print " raised to "
li $v0,1
move $a0,$t1
syscall #print Y
li $v0,4
la $a0,result2
syscall #print " gives "
li $v0,1
move $a0,$t2
syscall #print result(t2)
li $v0,10
syscall #exit
Read More

Converter numa Base diferente

.data
msg1: .asciiz "Inserir Valor (A > 0) : "
msg2: .asciiz "Inserir o valor para converter
(2<=B<=10): "

msg3: .asciiz "\nResultado : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:

li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA

move $t0,$v0
getB:

li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB

add $t1,$zero,$v0

li $v0,4
la $a0,msg3
syscall

add $a0,$zero,$t0
add $a1,$zero,$t1

jal convert

li $v0,10
syscall

convert:
#a0=A
#a1=B

addi $sp,$sp,-16

sw $s3,12($sp) #contador, usado

sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)

add $s0,$zero,$a0
add $s1,$zero,$a1

beqz $s0,end

div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3

add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert #call convert

end:

lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done:
addi $sp,$sp,20
jr $ra #return
Read More

Comparar Strings

.data
msg1:.asciiz "Please insert text (max 20 characters): "
msg2:.asciiz "\nNOT SAME"
msg3:.asciiz "\nSAME"
str1: .space 20
str2: .space 20
.text
.globl main
main:
addi $v0,4
la $a0,msg1
syscall
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall #got string 1
li $v0,4
la $a0,msg1
syscall
li $v0,8
la $a0,str2
addi $a1,$zero,20
syscall #got string 2

la $a0,str1 #pass address of str1
la $a1,str2 #pass address of str2
jal strcmp #call strcmp

beq $v0,$zero,ok #check result
li $v0,4
la $a0,msg2
syscall
j exit
ok:
li $v0,4
la $a0,msg3
syscall
exit:
li $v0,10
syscall

strcmp:
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
loop:
lb $t3($t1) #load a byte from each string
lb $t4($t2)
beqz $t3,checkt2 #str1 end
beqz $t4,missmatch
slt $t5,$t3,$t4 #compare two bytes
bnez $t5,missmatch
addi $t1,$t1,1 #t1 points to the next byte of str1
addi $t2,$t2,1
j loop

missmatch:
addi $v0,$zero,1
j endfunction
checkt2:
bnez $t4,missmatch
add $v0,$zero,$zero

endfunction:
jr $ra
Read More

Menor de 3 numeros

.data
val1: .word 0x10
val2: .word 0x35
val3: .word 0x16
max: .word 0
# aux1 reside no registo $t1 do CPU
.text
.globl main
main:
la $t0, val1 # $t0 = val1
lw $t1, 0($t0) # aux1 = val1
lw $t2, 4($t0) # aux2 = val2
lw $t3, 8($t0) # aux3 = val3
bge $t2,$t1,endif # if( aux2 > aux1) {
move $t1,$t2 # aux1 = aux2
# }
endif: bge $t3,$t1, endfor
move $t1, $t3

endfor: sw $t1, 12($t0)


jr $ra
Read More

Numeros Primos

.data
array: .space 80
.text
.globl main

main: addu $s0,$ra,$0
lui $s1,0x1001
addi $s2,$s1,80
addi $t0,$0,2
addi $t5,$0,1
sw $t0,0($s1)
addi $s1,$s1,4

loop1: addi $t0,$t0,1
or $t1,$t0,$0

loop2: addi $t1,$t1,-1
slt $t4,$t5,$t1
bne $t4,$0,skip
sw $t0,0($s1)
addi $s1,$s1,4
beq $s1,$s2,exit
j loop1
skip: div $t0,$t1
mfhi $t3
bne $t3,$0,loop2
j loop1

exit: addu $ra, $0, $s0
jr $ra
Read More

Temperatura

.data

prompt: .asciiz "\n Introduzir Temperatura:" #string
confirm: .asciiz "\n Em Fahrenheit é : " #String
display1: .asciiz "\n Em centígrado é:" #string


fhtTemp: .word 0 #var1
CentiTemp: .word 0 #var2

.text
.globl main

main:
li $v0,4 #teste a primeira
la $a0,prompt #String
syscall #apresenta no ecra

li $v0,5
syscall
sw $v0,fhtTemp

li $v0,4
la $a0,confirm
syscall

li $v0,1
lw $a0,fhtTemp
syscall


#convert

lw $t1,fhtTemp
li $a1,32
sub $t2, $t1, $a1
li $a1,5

mult $t2,$a1
mflo $t3
li $a1,9
div $t4, $t3, $a1

#end

li $v0,4
la $a0, display1
syscall

li $v0,1
move $a0,$t4
syscall

li $v0,10
syscall
Read More

Inverter String

.data
.align 1
String: .space 14 #*change
msg1: .asciiz "Introduzir caracteres: "
msg2: .asciiz "\n"
msg3: .asciiz "String is: "
msg4: .asciiz "\nString Invertida: "
.text
.globl main
main:

addi $s0,$zero,13 #*change
addi $t0,$zero,0

in:
la $a0,msg2
li $v0,4
syscall

li $v0,4
la $a0,msg1
syscall
li $v0,12
syscall

add $t1,$v0,$zero
sb $t1,String($t0)
addi $t0,$t0,1
slt $t1,$s0,$t0
beq $t1,$zero,in

sb $zero,String($t0) #ending zero

la $a0,msg2
li $v0,4
syscall
la $a0,msg2
li $v0,4
syscall
la $a0,msg3
li $v0,4
syscall

la $a0,String
li $v0,4
syscall

addi $a1,$zero,14 #pass length-*change
jal stringreverse #reverse

la $a0,msg2
li $v0,4
syscall

la $a0,msg4
li $v0,4
syscall

la $a0,String
li $v0,4
syscall

li $v0,10
syscall


stringreverse:

add $t0,$a0,$zero #beginning address

add $t1,$zero,$zero #i=0
addi $t2,$a1,-1 #j=length-1

loop:

add $t3,$t0,$t1
lb $t4,0($t3) #lb String[i]

add $t5,$t0,$t2
lb $t6,0($t5) #lb String[j]

sb $t4,0($t5) #String[j]=String[i]
sb $t6,0($t3) #String[i]=String[j]

addi $t1,$t1,1 #i++
addi $t2,$t2,-1 #j--
#if i>=j break - $t1<$t2
slt $t6,$t2,$t1
beqz $t6,loop

jr $ra
Read More

Ordenar

.data
.align 2
lista: .space 40 #int lista[10]
prompt: .asciiz " Insira um inteiro " #char prompt[18]

.text
.globl main

#le a lista dos numeros a ordenar

main: la $a3,lista #a3=lista
li $t0,0 #i=0
readlp: la $a0,prompt
li $v0,4
syscall #puts prompt
li $v0,5
syscall #geti v0
sw $v0,0($a3)
add $a3,$a3,4
add $t0,$t0,1 #i=i+1
slt $t1,$t0,10 #if(t0<10) t1=1
bnez $t1,readlp
# ordenaçao da lista
li $t0,0 # i=0
for1: bge $t0,9,end_for1 # while (i<9)
la $s0,lista
sll $s1,$t0,2 #s1=i*4
add $s0,$s0,$s1 #s0=&(lista[i])
addi $t1,$t0,1 # j=i+1

for2: bge $t1,10,end_for2 # while (j<10)
la $s2,lista
sll $s1,$t1,2 #s1=j*4
add $s2,$s2,$s1 #s0=&(lista[j])
lw $t2,0($s0) # aux=lista[i]
lw $t3,0($s2) # aux1=lista[j]
if: ble $t2,$t3,end_if # if (aux>aux1)
sw $t3,0($s0) # lista[i]=aux1
sw $t2,0($s2) # lista[j]=aux

end_if: addi $t1,$t1,1 # j++
b for2
end_for2: addi $t0,$t0,1 # i++
b for1
end_for1: li $v0,10 # exit
syscall
Read More

Combinações / Factorial

#--------------------------------------------------
# codigo em linguagem c
#--------------------------------------------------
# main(void)
# {
# char s[20];
# int n,p,cmb;
# printf("Insira o n: ");
# get (s,20);
# n = atoi(s,20);
# printf(" Insira o p ");
# get(s,20);
# p = atoi(s,20);
# cmb = comb(n,p);
# printf(" combinações = ");
# itoa(comb,s,20); printf(s);
# }
#--------------------------------------------------
# int comb(int n, int p)
# {
# return fact(n) / (fact(p) * fact(n-p)
# }
#---------------------------------------------------
# int fact (int n) /* algoritimo não - recursivo */
# {
# int i;
# int fct = 1;
# if (n<=1)
# return 1;
# else
# for (i=2; i<=n; i++)
# fct = fct * i;
# return fct;
#---------------------------------------------------
.data
str: .space 20
prompt1: .asciiz"\nInsira o n: "
prompt2: .asciiz"\ninsira o p: "
prompt3: .asciiz"\nCombinações = "
prompt4: .asciiz"\nResto="
.text
.globl main
main: la $a0,prompt1
li $v0,4
syscall
la $a0,str #
li $v0,8 #
li $a1,20 # Leitura de n
syscall
jal atoi # invocação da função ATOI
move $s0,$v0 # n=atoi(s,20)=$s0
la $a0,prompt2
li $v0,4
syscall
la $a0,str
li $v0,8
li $a1,20 # Leitura de p
syscall
jal atoi # invocação da função ATOI
move $a1,$v0 # p=atoi(s,20)=$s1
move $a0,$s0
jal comb # leitura de Combinações(n,p)
move $s2,$v0 # $s2=comb(s,20)=cmb
la $a0,prompt3
li $v0,4
syscall
add $a0,$0,$s2
la $a1,str
li $a2,20
jal itoa # Invocação da função Itoa
la $a0,str
li $v0,4
syscall
li $v0,10
syscall
#-------------------------------------------------------------------------
# Função ATOI (Converte string para inteiro)
#
# Parametros de entrada n--> Tamanho da string
# *s-> Array de string
# Retorno num-->Nº imteiro $v0=$t4
#------------------------------------------------------------------------
atoi: addi $sp,$sp,-4
sw $ra,0($sp) # salvaguardar os registos
li $v0,0 # num=0
add $t1,$0,$a0 # p=s
add $t2,$a0,$a1 # s+n
while: lb $t3,0($t1) # *p
bgeu $t1,$t2,exit1 # if (p>=s+n) go to exit1
blt $t3,'0',exit1 # if (*p<'0') go to exit1
bgt $t3,'9',exit1 # if (*p>'9') go to exit1
sub $t4,$t3,'0' # digito= *p-'0'
mul $v0,$v0,10 # num=10*num
add $v0,$v0,$t4 # num=10*num+digito
addiu $t1,$t1,1
j while
exit1: lw $ra,0($sp)
addi $sp,$sp,4 # Repor os registos
jr $ra
#-----------------------------------------------------------------
# Função ITOA (Convert inteiro para string)
#
# Parametros de entrada n--> Nº inteiro
# s--> Array de strings para retorno
# Retorno --> String de caracteres
#-----------------------------------------------------------------
itoa: addi $sp,$sp,-4
sw $ra,0($sp) # salvaguardar os registos
li $t1,10
move $t0,$a0
move $t3,$a1
li $t4,0 # i=0
move $t5,$a0 # j=n
do: div $t5,$t1
mfhi $t2 # parte concernente ao resto
mflo $t0 # parte concernente ao quociente
add $t2,$t2,'0' # resto + '0'
sb $t2,0($t3)
addiu $t3,$t3,1
addi $t4,$t4,1
move $t5,$t0 # $t5=Quociente
bnez $t0,do
sb $0,($t3) # coloca caracter nulo
#----------------------------------------------------------------------
# procedimento troca
#-----------------------------------------------------------------------
move $t1,$t3 # ponteiro para o fim do array
addi $t1,$t1,-1
move $t2,$a1 # ponteiro para o inicio do array
if: ble $t1,$t2,fim # if($t1<=$t2) go to fim
lb $t4,0($t1)
lb $t5,0($t2)
sb $t5,0($t1)
sb $t4,0($t2)
addi $t2,$t2,1
addi $t1,$t1,-1
j if
fim: lw $ra,0($sp)
addi $sp,$sp,4 # Repor os registos
jr $ra
#-------------------------------------------------------------------
# Função Calculo de combinações
#
# Parametros de entrada : n,p
# Parametros de saida : cmb= n!/(p!(n-p)!)
#---------------------------------------------------------------------
comb: addi $sp,$sp,-12
sw $ra,0($sp)
sw $s0,4($sp)
sw $s1,8($sp) # salvaguardar os registos
move $s0,$a0 # n
move $s1,$a1 # p
sub $a0,$s0,$s1 # n-p
jal fact1
move $a0,$s1
move $s1,$v0 # fact(n-p)
jal fact1
mul $s1,$s1,$v0 # $s1=fact(p)*fact(n-p)
move $a0,$s0
jal fact1 # fact de (n)
div $v0,$v0,$s1 # fact(n) / fact(p)*fact(n-p)
lw $s1,8($sp) # Repor os registos
lw $s0,4($sp)
lw $ra,0($sp)
addi $sp,$sp,12
jr $ra
#-------------------------------------------------------------------------------------
# Função Factorial (não recursivo)
#
# Parametros de entrada : inteiro
# Parametros de saida : fact=fact*i
#-------------------------------------------------------------------------------------
fact1: li $v0,1 # fct=1
bgt $a0,$v0,fim1 # if(n<=1)
jr $ra # return fct
fim1: li $t0,2 # i=2
for: bgt $t0,$a0,fim2 # while (i <= n)
mul $v0,$v0,$t0 # fct=fct*i
addi $t0,$t0,1 # i++
b for
fim2: jr $ra # return fct
Read More

O maior de 3

.data

prompt1: .asciiz"introduza os três numeros:\n"
resultado: .asciiz"o maior é: "
.text
.globl main

main:
la $a0,prompt1
li $v0,4
syscall
li $v0,5
syscall
move $t0,$v0

li $v0,5
syscall
move $t1,$v0

li $v0,5
syscall
move $t2,$v0

sge $s0,$t0,$t1
else: bnez $s0,if1
sge $s0,$t1,$t2
bnez $s0,else1
move $t4,$t2 # max=$t2
j fim
else1: move $t4,$t1 # max=$t1
j fim

if1: sge $s0,$t1,$t2
bnez $s0,if2
sge $s0,$t2,$t0
bnez $s0,if3

if2: move $t4,$t0 # max=$t0
j fim
if3: move $t4,$t2 # max=$t2
j fim
fim: la $a0,resultado
li $v0,4
syscall
li $v0,1
move $a0,$t4
syscall
Read More

Pensa num número

##################################################################
#
# Programa em linguagem C
#
# void main(void)
# {
# char prompt[] = "1. Pensa num numero!\n2. Adiciona 3\n"
# "3. Multiplica o resultado por 2\n"
# "4. Subtrai o numero em que pensaste\n"
# "\n\t Qual o resultado? ";
# char result[] = "\nO numero em que pensaste e': ";
# int num;
#
# print_str( prompt );
# num = read_int();
# print_str( result );
# print_int( num - 6 );
# }
#
##################################################################
.data
prompt: .ascii "1. Pensa num numero!\n"
.ascii "2. Adiciona 3\n"
.ascii "3. Multiplica o resultado por 2\n"
.ascii "4. Subtrai o numero em que pensaste\n"
.asciiz "\n\tQual o resultado? "
result: .asciiz "\nO numero em que pensaste e': "
##################################################################
.text
.globl main
# int num; "num" reside no registo $t0
#
main: la $a0, prompt # $a0 = prompt
li $v0, 4 # $v0 = 4 (syscall "print_str")
syscall # print_str( prompt )
#
li $v0, 5 # $v0 = 5 (syscall "read_int")
syscall # read_int() (o valor lido é
# devolvido no reg. $v0)
move $t0, $v0 # $t0 = $v0 ( num = read_int() )
#
la $a0, result # $a0 = result
li $v0, 4 # $v0 = 4 (syscall "print_str")
syscall # print_str( result )
#
sub $a0, $t0, 6 # $a0 = $t0 - 6 ( $a0 = num - 6)
li $v0, 1 # $v0 = 1 (syscall "print_int")
syscall # print_int( num - 6 )
#
jr $ra # abandona o programa
#########################################
Read More

Calculo o maior dentro de uma STRING

.data
Lista: .word 10 30 15 27 94 40 1 4 9 20
max: .space 4

.text
.globl main

main: la $t0, Lista #t0=Lista[0]
lw $t1, 0($t0) #t1=Lista[0]
li $t2, 0 #i=0

For: bgt $t2,10,endfor
mul $t3, $t2, 4 #t3=4*i para andar de 4 em 4
add $t3, $t3, $t0 # adiciona o t3 para o t0 e guardo no t3

lw $t4,0($t3)
ble $t4, $t1, endif
move $t1, $t4

endif: add $t2, $t2, 1 #i++

j For

endfor: la $t4,max
sw $t1, 0($t4)

jr $ra
Read More

Virgula Flutuante

int main(void)
{
float res;
int val1;
val1 = 35;
res = (float)val1 / 100.0;
print_flat(res);
return 0;
}


.data
.text
.globl main

main:
li.s $f6, 10.0 #f6 =100.0
li $t0,35 #val=35
mtc1 $t0, $f4
cvt.s.w $f4, $f4 #float(val1)

div.s $f12, $f4, $f6 #res=float(val1)\100.0
li $v0,2 #print.float(res)
syscall
li.s $f0,0.0
jr $ra
Read More

Tabuada

.data

prompt1: .asciiz" ### TABUADA ###\n\n"
prompt2: .asciiz"\n Qual a tabuada que deseja ver "
prompt3: .asciiz" x "
prompt4: .asciiz" = "
prompt5: .asciiz" \n"

.text
.globl main
main:

la $a0, prompt1 #solicita a introdução do número
li $v0, 4
syscall

la $a0, prompt2 #solicita a introdução do número
li $v0, 4
syscall

li $v0, 5 #Lê o número do teclado
syscall
move $t1, $v0

#################################

li $t0, 1 # i=1


for: bgt $t0, 10, endfor

move $a0,$t1
li $v0, 1 #
syscall #

la $a0, prompt3 # x
li $v0, 4
syscall

move $a0,$t0
li $v0, 1 #
syscall #


la $a0, prompt4 # =
li $v0, 4
syscall



mul $t2, $t0, $t1
move $a0,$t2
li $v0, 1 #
syscall #


la $a0, prompt5 # \n
li $v0, 4
syscall

endif: add $t0, $t0, 1 # i++
j for
endfor:


#####################################

li $v0, 5 #Lê o número do teclado
syscall
move $a0, $v0

jr $ra
Read More
 
Powered by Blogger