Oppure

Loading
05/09/12 10:42
shine1010
ciao ho trovato questo codice nelle sorgenti del sito, cè qualche anima buona che me lo può spiegare poichè sono un profano in assembly, mi bastano anche dei semplici commenti sulle operazioni più importanti grazie in anticipo

# Conversione da esadecimale a decimale
# @author Riccardo Morico

    .data

# program output text constants
prompt:     .asciiz  "\nInserire un numero esadecimale:  "
result1:    .asciiz  "Il suo decimale equivalente e: "
newline:    .asciiz  "\n"
myString:
    .space 64

    .text

#MIPS Variables for hexVal
# 


# main program
#
# program variables
#   num:   $s0

main:

while:
    li      $v0, 4          # issue prompt
    la      $a0, prompt
    syscall

    li      $v0, 8
    la      $a0, myString
    li      $a1, 64
    syscall

	la $s0, myString #$s0 = char *ptr = myString;
	lbu $s1, ($s0)  #$s1 = myString[0];
	beq $s1, 10, endw

	move $a0, $s0
	jal readHex

	beq $v1,0,notAHexValue #test result
	move $s0, $v0
	
    li      $v0, 4          # print "Il decimale equivalente è"
    la      $a0, result1
    syscall

    li      $v0, 1          #print result
    move    $a0, $s0
    syscall

notAHexValue:
	b while
endw: 

    li      $v0, 10         # terminate the program
    syscall


readHex:
	move $t8, $ra
	li $t3, 0
    li $t4, 0
    li $t5, 1
	li $t7, 16
hexWhile:
	lbu $t6, ($a0)
    beq $t6,10,hexEndWhile
    mul $t3, $t3, $t7
    
    lbu $v0, ($a0)
    jal hexVal

	beq $s1, -1, failed

    add $t3, $t3, $s1
    
	add $a0, $a0, 1 #str++
    b hexWhile
failed:
	li $t5, 0
hexEndWhile:
	move $v0, $t3
    move $v1, $t5
	jr $t8


hexVal:
	bge $v0, 48, greaterEqualThan0 #if ((ch1 >= '0'))
    b elseIfChGreaterEqualA
greaterEqualThan0: 
	ble $v0, 57, lessEqualThan9
	b elseIfChGreaterEqualA
lessEqualThan9: # && (ch <= '9')
	sub $t0, $v0, 48
	b endif	

elseIfChGreaterEqualA: #else if (ch >= 'a')
	bge $v0, 97, greaterEqualThanALower
	b elseIfChGreaterEqualThanAUpper
greaterEqualThanALower: #&& (ch <= 'f')
	ble $v0, 102, lessThanEqualFLower
	b elseIfChGreaterEqualThanAUpper
lessThanEqualFLower:
	sub $t0, $v0, 97
	add $t0, $t0, 10
	b endif

elseIfChGreaterEqualThanAUpper: #else if (ch >= 'A')
	bge $v0, 65, greaterEqualThanAUpper
	b else
greaterEqualThanAUpper: #&& (ch <= 'F')
	ble $v0, 70, lessThanEqualFUpper
	b else
lessThanEqualFUpper:
	sub $t0, $v0, 65
	add $t0, $t0, 10
	b endif
else:
	li $t0, -1
endif:
	move $s1, $t0
	jr $ra

Ultima modifica effettuata da shine1010 05/09/12 10:43
aaa