Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ramazme

macrumors newbie
Original poster
Sep 17, 2008
5
0
i, i need help with self modifying code.
i have written a program that does some operations once, except once the result is 7, program will have to modify itself so that from then on entering addition will instead give the logical AND of the two operands.

add $s3,$s0,$s1
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
i, i need help with self modifying code.
i have written a program that does some operations once, except once the result is 7, program will have to modify itself so that from then on entering addition will instead give the logical AND of the two operands.

add $s3,$s0,$s1

i desperately need help asap, thank you

is this for a class? Are you sure this needs to be self-modifying, or just act conditionally?

-Lee
 

ramazme

macrumors newbie
Original poster
Sep 17, 2008
5
0
yea this is for a class, well once the answer is 7 it loops back and asks them a question now every time they try to add it does logical and. and yes it is supposed to self modifying code and not if else:(
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
i, i need help with self modifying code.
i have written a program that does some operations once, except once the result is 7, program will have to modify itself so that from then on entering addition will instead give the logical AND of the two operands.

add $s3,$s0,$s1

i desperately need help asap, thank you

It seems you don't actually mean "I have written a program" but "my homework assignment is to write a program". There are a few obstacles here. To mention a few:

1. Helping you to cheat on your homework assignment isn't helpful in the long run, so hopefully nobody will do it.
2. There is no version of the Macintosh that runs MIPS instructions, so posting on MacRumors doesn't have much of a chance.
3. Recent MacOS X versions make it difficult to write self modifying code.

Ok, I'll give you some help at least:

1. When posting homework questions, try putting your question into proper English so that people can figure out what the actual question is, without giving away that it is homework.
2. Try finding other students who are willing to work together with you.
3. What about googling for "mips self modifying code"?
 
  • Like
Reactions: Basic75

ramazme

macrumors newbie
Original poster
Sep 17, 2008
5
0
the assignment is to actually write a calculator program. it basically takes some input binary,decimal,hex and does 4 operations +,-,*,/ but there is a special case where when the result is 7 the program will modify itself so that from then on entering addition will instead give the logical AND of the two operands.
I dont know anything about self modification.
 

ChrisA

macrumors G5
Jan 5, 2006
12,900
2,110
Redondo Beach, California
i, i need help with self modifying code.
i have written a program that does some operations once, except once the result is 7, program will have to modify itself so that from then on entering addition will instead give the logical AND of the two operands.

add $s3,$s0,$s1

i desperately need help asap, thank you

Yes, there is still a need to write self modifying code. Sometimes you are fixing code or adding some new feature for a micro controller or some other tiny computer in assembly and the ROM is full. Also Sun's D-trace which I think is comming to a Mac near you soon uses self modifying code. It is faster then using an if statment.

How to do it? two methods: (1)place jump instruction some place. Make the target address just the next instruction. The after counting to 7 over write the target address with the start of the routine that does the loggical AND.

Or a second method, the one I bet your teacher is looking for is simpler, simple over write the op code for the AND with the op code for the ADD.

But the first method is more general purpose
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
http://pages.cs.wisc.edu/~larus/spim.html

If you don't have one for OS X already, go here and grab the SPIM emulator for OS X. I would personally approach this by using an offset from the stack pointer, or storing the location of the add instruction in a known position in memory, then you just need to either have the instruction you need to substitute in memory or as a literal in your ASM that you will conditionally move in to the position of the add. You most likely only need to change the first 5 bits where the opcode is stored, but it's been 4-5 years since i've written MIPS assembly.

Take care that you can switch it back, if that's necessary.

-Lee

Edit: Erm, also if you desperately need help you should speak to your professor or TA.
 

ramazme

macrumors newbie
Original poster
Sep 17, 2008
5
0
thank you:) i tired asking them for help but i just didn't understand their explanation. i usually learn thru examples. he said the same thing overwriting oop code i dont know how to do it :(
 

ramazme

macrumors newbie
Original poster
Sep 17, 2008
5
0
oh well its alright i did 90% of this project,i don't know about this little part i spent way too much time on this project already. but thank you guys :)
 

ChrisA

macrumors G5
Jan 5, 2006
12,900
2,110
Redondo Beach, California
oh well its alright i did 90% of this project,i don't know about this little part i spent way too much time on this project already. but thank you guys :)

Your problem, I think is that you don't understand how assembly language source code is converted to machine code. This is a very, very fundamental concept and you are going to have to understand this. You are going to have to know what gets loaded into RAM. Once you do over writing any given byte with a constant is trivial.
 
  • Like
Reactions: Basic75

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Because I jump on anything that sounds interesting that I haven't dealt with in a while, i grabbed spim, got xspim compiled, and started playing with this. I have a working copy that changes:
Code:
add $t3,$t1,$t2
into
Code:
and $t3,$t1,$t2

I did it explicitly, rather than just masking the opcode bits because it was easier. It would only be a few more instructions, but I didn't feel up to it. The program does one add, based on its result does a bne and in one case will do another add, or it will change the add to an and then jump back and perform that once.

I won't post the code because this is homework, but the essentials are:
1) have the instruction you want to load in memory.
2) load that thing into a register
3) have a way to get the address of the line you need to replace. this is easy in ASM.
4) move the instruction from the register in step 2 into the memory position from step 3 under the appropriate condition.

The main instructions involved here are load word, store word, and... well, that's really it. It sounds like you have the rest already. As ChrisA said, it's crucial to be able to write instructions (well, at least one instruction) in machine code to do this, not just assembly. MIPS is a very gentle ISA to work with when you're learning, so it shouldn't be too bad. Like I said, i haven't looked at MIPS in 5 years, and I got xspim compiled 1 hour and 20 minutes ago, and I've been watching television while working on this.

I guess I can't be sure when the project due date has passed to post the code, but I'll take your word for it if you say it has passed in a few days and I can post the code. Hopefully by then the professor will have showed you an example, though.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I'm assuming the assignment is long past due now, and thought I'd post this before I forgot it:
Code:
example:
	.data
andins:	.word  0x012A5824 #This is and $t3,$t1,%t2
#0000 0001 0010 1010 0101 1000 0010 0100
space:	.asciiz " "

	.text

main:
	lw	$t0, andins #This sets up the real action
	li	$t1, 5
	li	$t2, 2
	li	$t4, 7
calc:   add	$t3,$t1,$t2
	bne	$t3,$t4,moreadd
	sw	$t0, calc #This is where the real action is
	li	$t1,2
	li	$t2,1
	li	$t4,50 # This should equal 0
	j	calc
moreadd:
	li	$t5, 13
	beq	$t3,$t5,end
	li	$t5, 0
	beq	$t3,$t5,end
	li	$t1, 4
	li	$t2, 9
	j	calc

end:
	li 	$v0, 1
	move	$a0, $t1
	syscall
	li	$v0, 4
	la	$a0, space
	syscall
	li	$v0, 1
	move	$a0, $t2
	syscall
	li	$v0, 4
	la	$a0, space
	syscall
	li	$v0, 1
	move	$a0, $t3
	syscall
	li	$v0, 4
	la	$a0, space
	syscall
	li	$v0, 1
	move	$a0, $t4
	syscall
	li	$v0, 4
	la	$a0, space
	syscall
	li	$v0, 1
	move	$a0, $t5
	syscall
	done

Everything after the end label is just printing out the register contents for debugging.

The code was structured to just perform each operation once, but i think it demonstrates the easiest method I could think of for self-modification.

Like I said earlier, hopefully the professor gave you a (better) example by now, but I wanted to post this before I forgot in case it's of interest.

-Lee
 

txcvhj76

macrumors newbie
Feb 19, 2021
1
0
I'm having trouble writing a MIPS program in MARS that add, subtracts, and multiplies arrays without using loops and to print this output:

arr3 = arr1 + arr2
arr4 = arr1 - arr2
arr5 = arr1 + 4
arr6 = arr2 - 8
arr7 = values enterd by user
arr8 = arr1 * arr2

Here's my code of i have so far:

la $t1, arr1
la $t2, arr2
add $a0, $t1, $t2

syscall
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.