Trying to make stdout work
This commit is contained in:
		
							parent
							
								
									e681b848d5
								
							
						
					
					
						commit
						0e259f27bd
					
				
					 1 changed files with 70 additions and 9 deletions
				
			
		
							
								
								
									
										79
									
								
								main.s
									
										
									
									
									
								
							
							
						
						
									
										79
									
								
								main.s
									
										
									
									
									
								
							|  | @ -1,12 +1,73 @@ | ||||||
| .section .text | # Define variables | ||||||
| .globl _start
 | .data | ||||||
|  | .align 4
 | ||||||
|  |     num1: .word 5     # First number | ||||||
|  |     num2: .word 7     # Second number | ||||||
|  |     result: .word 0   # Variable to store the result | ||||||
|  |     msg: .ascii "Hello, World!\n" | ||||||
|  | 
 | ||||||
|  | # Main program entry point | ||||||
|  | .text | ||||||
|  | .global _start
 | ||||||
|  | .align 4
 | ||||||
|  | test_routine: | ||||||
|  |     la a0, num1    # Load the address of num1 into register a6 | ||||||
|  |     li a1, 10      # Load the value 10 into register a5 | ||||||
|  |     xori a2, a1, 0x1  # Calculate the value of a2 | ||||||
|  |     sw a1, 0(a0)    # Store the value in memory | ||||||
|  |     ret | ||||||
| 
 | 
 | ||||||
| _start: | _start: | ||||||
|     # Call the hello function |     call h2 | ||||||
|     lui a0, %hi(_hello)    # Load upper immediate of _hello address |     call test_routine | ||||||
|     addi a0, a0, %lo(_hello)  # Add lower immediate of _hello address |     call hello | ||||||
|     jalr ra, a0, 0          # Jump and link to _hello, ra is return address | 
 | ||||||
|  |     # Load the first number into register t0 | ||||||
|  |     lw t0, num1 | ||||||
|  | 
 | ||||||
|  |     # Load the second number into register t1 | ||||||
|  |     lw t1, num2 | ||||||
|  | 
 | ||||||
|  |     # Add the two numbers and store the result in register t2 | ||||||
|  |     add t2, t0, t1 | ||||||
|  | 
 | ||||||
|  |     # Store the result in memory | ||||||
|  |     la t3, result | ||||||
|  |     sw t2, 0(t3) | ||||||
|  | 
 | ||||||
|  |     # Call _hello | ||||||
|  |     la a0, result | ||||||
|  | 
 | ||||||
|  |     # End program | ||||||
|  |     ecall | ||||||
|  | 
 | ||||||
|  | h2: | ||||||
|  |     addi a0, x0, 0x68 | ||||||
|  | 	li a1, 0x10000000 | ||||||
|  | 	sb a0, (a1) # 'h' | ||||||
|  | 
 | ||||||
|  | 	addi a0, x0, 0x65 | ||||||
|  | 	sb a0, (a1) # 'e' | ||||||
|  | 
 | ||||||
|  | 	addi a0, x0, 0x6C | ||||||
|  | 	sb a0, (a1) # 'l' | ||||||
|  | 
 | ||||||
|  | 	addi a0, x0, 0x6C | ||||||
|  | 	sb a0, (a1) # 'l' | ||||||
|  | 
 | ||||||
|  | 	addi a0, x0, 0x6F | ||||||
|  | 	sb a0, (a1) # 'o' | ||||||
|  |     ret | ||||||
|  | 
 | ||||||
|  | hello: | ||||||
|  |     # Write the string "Hello, World!\n" to stdout | ||||||
|  |     la a0, msg              # Load the address of the string into a0 | ||||||
|  |     li a7, 4                # syscall number for write | ||||||
|  |     li a1, 13               # Length of the string | ||||||
|  |     li a2, 1                # File descriptor: stdout | ||||||
|  |     ecall                   # Make the system call to write | ||||||
|  |     ret                     # Return from the function | ||||||
|  | 
 | ||||||
|  | # To avoid the program from exiting | ||||||
|  | loop: j loop | ||||||
| 
 | 
 | ||||||
|     # Exit the program |  | ||||||
|     li a7, 10               # syscall number for exit |  | ||||||
|     ecall                   # Make the system call to exit |  | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus