![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
MIPS assembly language
i need help writing a program in the MIPS assembly language. I have to write a program that finds the hamming distance between 2 binary numbers. The hamming distance is the number of bits that are different in 2 binary numbers. So i was thinking first creating 2 integer arrays and then prompting for the 2 numbers. I am not sure if the numbers will be entered in binary or if they will be entered in decimal. Then loop through the 2 arrays comparing the indexes of each of them. Then if the 2 bits are different have an array that would put a 1 in that index and if they are the same put a 0. Then loop through the third array and count the number of 1's thus giving the hamming distance. So that is my plan however, i was not given any references to this MIPS language or any examples of code. I have found a couple via google but if anyone knows how to declare arrays or make loops or fill arrays with user entered input or convert decimal numbers to binary i would appreciate it. Just by playing around i have figured out how to display a prompt (i guess that is a start) but i dont know how to save the entered information. I have had some experience with java and a little with C++ but i have never done anything in any type of assembly language before and it doesn't really make much sense to me. If anyone has any idea what im talking about or know any references for commands for MIPS that would be much apreciated. Thanks.
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
After 2 hours this is what i got and its not even anything. All I'm trying to do is print a prompt and store a number and it doesn't work right.
.text
main:
la $a0,prompt_in #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
la $a1,prompt_ina #print prompt on terminal
li $v1,4 #prints a string on the terminal
syscall
li $v1,5 #reads in an integer
syscall
li $v0,10 #exit program
syscall
.data
prompt_in: .asciiz "Enter first integer: " # first integer
prompt_ina: .asciiz "Enter second integer: " #second integer
prompt_out: .asciiz "The hamming distance is " |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
alright so i figured out how to get the 2 inputs. I also got some examples on looping through arrrays and stuff. Now i have to figure out how to convert them to binary and store the binary numbers into arrays which i have no clue how to do.
Here is what i have so far: .text
main:
la $a0,prompt_in #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
#move $a0,$v0
#li $v0,1 #prints integer
#syscall
la $a0,prompt_ina #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
#move $a0,$v0
#li $v0,1 #prints integer
#syscall
#convert numbers entered to binary:
#div $v0,$v0,2 #divide by 2
#li $v0,1
#syscall
li $v0,10 #exit program
syscall
.data
prompt_in: .asciiz "Enter first integer: " # first integer
prompt_ina: .asciiz "Enter second integer: " #second integer
prompt_out: .asciiz "The hamming distance is " |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'm not familiar with the MIPS; nor am I familiar with what your input provides. I suspect an integer digit is presented to you as an ASCII coded number. That means it's coded value will be 30 hex greater than its actual value. Subtract 30 hex from it to get the value, which IS binary, no need to 'convert'. Look at the XOR operator to detect which bits are different, then use your bit operators (whatever the MIPS has) or shift operators to determine what the bits are in the result.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
thanks for the response. my input is supposed to provide 2 integer numbers that are then switched to binary and stored in arrays which are then compared and if the numbers are the same a 0 is stored in that index in a third array and if they are different then a 1 is stored in that index in a third array. As for the subtracting and stuff. I dont think i understand. The user just enters any decimal number how is that an ascii coded number? I thought ascii was with text stuff? Also, how would taking the number that is entered and subtracting 30 hex from it make it all 1's and 0's? Also i dont think i was storing the numbers correctly i think it was just overwriting $v0. Here is the updated code also i can not figure out how to print this to make sure that num1 and num2 are being stored correctly. If someone knows if they are or not that would be apreciated.
.text
main:
la $a0,prompt_in #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
sw $v0,num1
#move $a0,$v0
#li $v0,1 #prints integer
#syscall
la $a0,prompt_ina #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
sw $v0,num2
#convert numbers entered to binary:
#la $s0,num1
#lw $t0,num1
#la $s1,num2
#lw $t1,num2
#div $t0,$t1,2 #divide by 2
#move $a0,$t0
#li $a0,1 #print integer
#syscall
li $v0,10 #exit program
syscall
.data
prompt_in: .asciiz "Enter first integer: " # first integer
prompt_ina: .asciiz "Enter second integer: " #second integer
prompt_out: .asciiz "The hamming distance is "
num1: .word 0
num2: .word 0 |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
From this code:
li $v0,5 #reads in an integer syscall #move $a0,$v0 If it is an integer, then you already have the binary. To get the individual bits out, it is just a matter of using "and"s and bit shifting calls. You don't really need to extract the bits and put them in an array and then compare them, you could just extract them and compare them as you go. Caveat: I've never done any MIPS assembly, so this is all an educated guess. |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
Alright so if the user enters a normal number like the number 4 then how do i already have the binary equivalent? Also, how would ands get me the individula bits.
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
o ok i see what your saying kind of i think. So you get the bits from the number thats entered and then you dont need arrays or anything because you can just use AND gates and if the AND is false then return a 1. Alright i kinda see what your saying but as far as doing it i have no clue. All this stuff with different registers and things and storing stuff in memory and loading words and picking the right register to use so it works still makes very little sense to me. Like i know registers are in the cpu and they do different things. Maybe since you guys don't know mips specifically these questions can't be answerd but i think thats what im most confused about is how to actually make it work. I am also confused with all the load word and load imediate and load address and all those too. I'm gonna try to ask a few questions that might clear it up. So if you look at this part of the code:
la $a0,prompt_in #print prompt on terminal li $v0,4 #prints a string on the terminal syscall li $v0,5 #reads in an integer syscall sw $v0,num1 la $a0,prompt_ina #print prompt on terminal
li $v0,4 #prints a string on the terminal
syscall
li $v0,5 #reads in an integer
syscall
sw $v0,num2 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
What do you suppose a 'normal number input' is? Your system deals in binary. The keyboard could put out anything (but it will be binary). Your driver will no doubt put it in a common (but binary) representation. Whether your mechanism provides an integer or coded integer is unknown to me. Here's an example: your keyboard give you 0x31 when you press the '1' key. That happens to be an ASCII representation for the digit, 1, since the binary value, 1, is generally used for something else that isn't printable.
ASCII '1' = 0x31 = 00110001b. See those bits? Perhaps, as The Dark mentions, your machine takes care of that and gives you the 00000001 directly, already converted. Less to do for you. Two binary values, say 01011010 and 01100101, can be XORed, which produces a 1 in the position if one and only one 1 is present. Bits which are alike, therefore, will produce a zero in the position. Therefore, if you count the zeroes, you know how many bits are different. You can count the bits in several ways. One way would be to rotate through the carry bit and check the state of that bit. If you'd rather work with ones, just invert the value. If you make an AND mask with one bit set, and AND it with the value, the result will be zero if that bit is not present, non-zero if it is present. Then move the bit in the mask, test and count the next position. As I say, there are a number of ways. Get familiar with your instruction set. EDIT. Sorry, this was posted before your most recent post. You will have to refer to your documentation to see if registers have some particularly specific purpose, or if they're general purpose. A load immediate means the value to be loaded is directly in the instruction, not out in memory. Loading direct means you load directly from a particular memory address or other register. That address is contained in the instruction. An indirect means you find the address elsewhere, in another register, or possibly in memory. Microprocessors of different kinds work in the same essential ways. Some have more powerful arithmetic or addressing methods, or more registers, or whatever. Again, you have to refer to the docs for the device you're using. A general purpose understanding of micros in general could be beneficial, however.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 344
Rep Power: 4
![]() |
alright so if when i input the number its already in binary? So when it prompts for the first input and i say 4 and then have it print that input and it prints 4 how do i get the binary equivalent of that?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assembly Language for Intel-Based Computers | melbolt | Book Reviews | 0 | Aug 15th, 2006 12:04 AM |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 5:45 PM |
| More languages? | UnKnown X | Coder's Corner Lounge | 27 | Dec 18th, 2005 3:06 PM |
| Does assembly language vary per machine? | Xero | Assembly | 9 | Jan 17th, 2005 11:34 AM |