Hamilton cycles of assembly language (there is a small bug)

C ++ implementation 
#include <iostream> #include <cstdio> #include <bits / STDC ++. H> / * adjacency matrix storage map, from the start, direct deep search, marking, has been found not to re-search and see n steps is not gone, and end to end * / int Graph [ 30 ] [ 30 ]; int Sign [ 30 ]; int n, m, ANS = 0 ; void the DFS ( int now, int sTEP); int main () { int U, V; Scanf ( " % D% D " , & n-, & m); // might vertices of the graph from a start number for ( int I = 0 ; I <m; I ++ ) { scanf("%d %d",&u,&v); graph[u][v]=graph[v][u]=1; } sign[1]=1; DFS(1,1); printf("%d\n",ans); return 0; } void DFS(int now,int step){ if(step==n){ if(graph[1][now]){ years = 1 ; } return; } for(int i=1;i<=n;i++){ if(!sign[i] && i!=now && graph[now][i]){ sign[i]=1; DFS (i,step+1); sign[i]=0; } } return; }
:( compilation includes a small bug, seems to be stuck single point or two points, but especially after the sentence was wrong but more a) 
code has been special treatment
.macro done li $ v0,
10 syscall .end_macro ############################################################# .macro initial li $ t0, 0 for_1: beq $t0,$s0,for_1_end sll $t1,$t0,2 #t1=t0*4(int is 4 byte) add $t1,$t1,$s3 sw $0,0($t1) #sign[t1]=0 addi $t0,$t0,1 #t0=t0+1 j for_1 for_1_end: .end_macro ############################################################# .macro getindex(%ans,%i,%j) mult %i,$t6 #i*10 mflo $t4 #t4=i*10 add %ans,$t4,%j #t4=i*10+j sll% year% year 2 # years = years * 4 .end_macro ############################################################# .data matrix: .space 400 sign: .space 40 ######################################################### .text main: la $s2,matrix the $ s3, sign $ T6, 10 #tmp li $t7,1 #tmp li $s7,0 #s7 is answer li $ v0, 5 SYSCAL move $s0,$v0 #s0 is n li $ v0, 5 SYSCAL move $s1,$v0 #s1 is m initial li $t0,0 #t0 is i for_create: beq $t0,$s1,for_create_end #if i==m,stop add edge li $ v0, 5 SYSCAL move $t1,$v0 #t1 is u li $ v0, 5 SYSCAL move $t2,$v0 #t2 is v getindex($t3,$t1,$t2) #get the address in matrix add $t3,$t3,$s2 #get address in matrix,t3 is address sw $t7,0($t3) #add edge (u,v) getindex ($ t3, $ t2, $ t1) add $t3,$t3,$s2 sw $t7,0($t3) #add edge (v,u) addi $t0,$t0,1 #i++ j for_create for_create_end: sw $t7,4($s3) #sign[1]=1 li $a0,1 #a0 is now position li $a1,1 #a1 is step jal dfs #the return address is in $31 li $v0,1 li $a0,0 syscall done dfs: sw $31,0($sp) addi $sp,$sp,-4 blt $a1,$s0,dfs_else getindex ($ t3, $ t7, $ a0) add $t3,$t3,$s2 #t3 is address in matrix lw $t4,0($t3) #t4=graph[1][now] beq $ t4 1 , arrive_ans addi $ sp, $ sp, 4 lw $ 31 , 0 ($ sp) jr $31 #if we don't find the ans,return #omit something………… dfs_else: jal dfs_work addi $sp,$sp,4 lw $ 31,0 ($ sp) jr $31 then: jr $31 #you have jumped to main succesfully arrive_ans: li $ s7,1 Does $ ± 0,1 li $v0,1 #print answer syscall done j then dfs_work: #then ,we should do something li $t0,1 #t0 is i sw $31,0($sp) #save return address addi $sp,$sp,-4 for_dfs1: bgt $t0,$s0,for_dfs1_end bge $a1,$s0,for_dfs1_end sw $a0,0($sp) #save now addi $sp,$sp,-4 sw $a1,0($sp) #save step addi $sp,$sp,-4 #all the info was saved sucessfully sll $ t1, $ t0,2 # t1 = t0 * 4 add $t1,$t1,$s3 #t1 is the address in array_sign lw $t2,0($t1) #t2 is sign[i] getindex($t3,$a0,$t0) #t3 is [now][i] lw $t3,0($t3) #t3 is graph[now][i] beq $t2,1,else #if sign[i]==1,return beq $t0,$a0,else #if i==now,return beq $t3,0,else #if graph[now][i]==0,return sw $t7,0($t1) #sign[i]=1; sw $t0,0($sp) #save tmpi in stack addi $sp,$sp,-4 sw $t1,0($sp) #save the address in array_sign addi $sp,$sp,-4 move $a0,$t0 #now=i addi $a1,$a1,1 #step++ jal dfs #next recursion addi $sp,$sp,4 lw $t1,0($sp) #fetch the address in array_sign addi $sp,$sp,4 lw $t0,0($sp) #fetch i sw $t7,0($t1) #sign[i]=0 addi $sp,$sp,4 lw $ a1,0 ($ sp) addi $sp,$sp,4 lw $a0,0($sp) #fetch all the things addi $t0,$t0,1 #i++ j for_dfs1 for_dfs1_end: addi $sp,$sp,4 lw $31,0($sp) #fetch the leeway jr $31 else: Addi $ T0, $ T0,l addi $sp,$sp,4 lw $ a1,0 ($ sp) addi $sp,$sp,4 lw $a0,0($sp) #fetch all the things j for_dfs1

 

Write a recursive program in assembly language lessons:

1. push and pop values: must pay attention to this, when each call other procedures, should not have to think about saving escape route down, the preservation of the original variables, and some even more than once saved, some even because the branch and to push the stack in each branch. Recursion is also much practice, this actually write for six hours was also a point card, I can see a high probability P2 will be killed.

2. The written judgment condition: very often with equal beq judgment and not enough (because too confusing to write their own programs, value more than expected), we need to be able to judge than or equal to, less than equal, less strict, rigorous instruction large, as in the end which one you use, be sure to think well when writing code. Loop exit conditions generally written at the top of the cycle. The best conditions are written on the back of each judge clearly notes.

3. Do not be afraid. Assembler looked long and repeated many of them are actually part of the stack into the stack, after the time of writing no longer crazy to smash things up!

Guess you like

Origin www.cnblogs.com/BUAA-Wander/p/11706452.html