Addition and subtraction in machine language

In summary, the program provided does not work as intended. The instructions provided do not produce the correct code.
  • #1
Spenman91
6
0
I'm working on an assignment for a computer science course. The professor did not give very good instruction, and I can't find very much information about the subject online. We are supposed to use this http://www.anne-gert.nl/projects/simpsim/ program to preform various operations including adding, subtracting, multiplying, and dividing values. If I just had something to read or an example of how to use the program, it would be very helpful.

For all intents and purposes, let's assume that I want to find the difference of two integers.

My Attempts:
List of Steps for Attempted Program Execution.
1. Place numeric values into memory locations
2. Load those values from the memory locations into different registers
3. Perform the "addi" operation to the values stored in the registers and place the result in another register.
4. Store the result from the register into a memory location
5. Use the "Halt" operation to stop the program.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Spenman91 said:
I'm working on an assignment for a computer science course. The professor did not give very good instruction, and I can't find very much information about the subject online. We are supposed to use this http://www.anne-gert.nl/projects/simpsim/ program to preform various operations including adding, subtracting, multiplying, and dividing values. If I just had something to read or an example of how to use the program, it would be very helpful.

For all intents and purposes, let's assume that I want to find the difference of two integers.

My Attempts:
List of Steps for Attempted Program Execution.
1. Place numeric values into memory locations
2. Load those values from the memory locations into different registers
3. Perform the "addi" operation to the values stored in the registers and place the result in another register.
4. Store the result from the register into a memory location
5. Use the "Halt" operation to stop the program.

Show us your assembly code (it's not machine code).
If you intend to find the difference of two integers, you don't want to use the addi opcode.

Your code should look something like this (taken from an example on the page whose link you provided):
Code:
         load    R1,Text     ;the start of the string
         load    R2,1        ;increase step
         load    R0,0        ;string-terminator
NextChar:load    RF,[R1]     ;get character and print it on screen
         addi    R1,R1,R2    ;increase address
         jmpEQ   RF=R0,Ready ;when string-terminator, then ready
         jmp     NextChar    ;next character
Ready:   halt

Text:    db      10
         db      "Hello world !",10
         db      "    from the",10
         db      "  Simple Simulator",10
         db      0           ;string-terminator

Please put a [noparse]
Code:
 tag at the top of your code and a
[/noparse] tag at the bottom, as I have done.

There should be some documentation with the assembler. In it you will find descriptions of all the op codes that are available for use. I'm guessing that one op code is named subi.

The doc pages for the op codes tell you what registers need to be used, and what register contains the result of the operation.
 
  • #3
We were never really taught how to write any code. We were told to use the program to create our programs visually. I should probably note that this is an introductory course that is meant to teach basic computer science and engineering fundamentals. The course is not centered around programming like some other CS courses.

This is the first question for the assignment.

Using machine language, subtract [A2] from [A1] and store in [A3]. (Perform A3 = A1 - A2 in machine language). Start your instructions at C0. Set memory locations as follows: A1=B0 and A2=03.

When I follow those instructions using the provided program I receive an "invalid instruction" error message. The code that the program produces when I click "assemble is this:

Code:
00:  00,00  invalid
              "
              "
A0:  00,B0  invalid
A2:  03,00  invalid
A4:  00,00  invalid
              "
              "
C0:  10,A1  load  R0,[A1]    ;...
C2:  11,A2  load  R1,[A2]    ;...
C4:  50,12  addi  R0,R1,R2   ;...
C6:  32,A9  store R2,[A9]    ;...
C8:  C0,00  halt             ;...
CA:  00,00  invalid
              "
              "

The program does not contain any function that appears to subtract. The list of commands:

invalid
direct load
immediate load
direct store
move
addi
addf
or
and
xor
ror
impEQ
halt
indirect load
indirect store
impLE

We are then supposed to use those functions to manipulate data in various memory locations and registers.

Sorry if anything is unclear. From what I gather, (I've searched for information on assignment in other places) this task is pretty unique and seems to only pertain to this course.

Any information or help you can provide is greatly appreciated. Thanks!
 
  • #4
I don't see any instructions for subtraction, either. Under Help --> Instructions, there are all of the available instructions, and what the machine code is. Apparently the assignment is to give the actual machine code instructions to do something.

Since the assignment asks you to subtract, and there is no subtraction operator, I would get in touch with the class instructor. It's possible that he/she wrote the assignment without realizing the capabilities of this software.
 
  • #5
I'll contact the instructor. The class is taught by a teaching assistant, so that is definitely a possibility.

I know that in class we discussed shifting a rotating to preform arithmetic operations in class, but I'm not familiar enough with those concepts to complete the assignment.

Thanks for taking the time to read and discuss my problem.
 
  • #6
If you have studied 2's complement notation, you might be expected to implement subtraction using 2's complement addition. To convert a negative binary to 2's complement form, XOR it with all 1's, then add 1.
 
  • #7
That is exactly what we are supposed to do! The TA's primary language is not English, and he is very difficult to understand sometimes. I'm not blaming him for the difficulty I'm having in the course, but it is a problem. I try to remedy this by reading about the concepts we cover in class online. I searched for material on this topic but didn't have much luck. Are there any websites you can think of that would help me learn this material (many of the topics we cover in class are not covered in our official course book)?

Thanks for the suggestion. It is definitely appreciated.
 
  • #8
I doubt you'll need to know how to swap two registers without using a third register, but just in case, it's done like this:

Code:
    xor R0,R1
    xor R1,R0
    xor R0,R1
 
  • #9
Spenman91 said:
The TA's primary language is not English, and he is very difficult to understand sometimes. I'm not blaming him for the difficulty I'm having in the course, but it is a problem.
Education involves learning on multiple planes. Just below "subject matter" is "mind reading", and next comes "do what I mean not what I said", then ... :smile:
Are there any websites you can think of that would help me learn this material (many of the topics we cover in class are not covered in our official course book)?
A google search should become almost a reflex action. That way you can choose whatever level of difficulty and presentation best appeals to you.

But why not experiment, based on nothing more than what I've told you?

Example. Use 8 bit registers and 2's complement arithmetic to show the steps involved in processing

47 + -19


Code:
        47 = 00101111 (notice how I'm filling all 8 bits--very important!)
        19 = 00010011, therefore using the XOR + 1 formula I mentioned,
       -19 = 11101100 + 1
           = 11101101

Now, use binary addition with  00101111 + 11110000
You know the answer has to be 28, so see whether that materializes.

Now try a few more, then I'd go and read up in detail on the topic.
These exercises will give you practice in binary conversion, too.

Good luck!
 
Last edited:
  • #10
I'm just dropping into thank everyone for the assistance. Unfortunately, I haven't had very much time to devote this subject lately due to the vast amount of work I've had to complete for my other courses.

I'm going to continue working on the assignment today and read up on the topic. The information you have provided should be sufficient enough for me to get started. I'll post back if I run into any problems.

Thanks again!
 
  • #11
I've been working on the assignment, and I watched a couple of videos about 2's complement and binary addition. I need to know those concepts as well, but I may have been confused when I stated that we were supposed to use that procedure to complete the assignment.

Now that I have a better understanding of what the assignment is asking, I'm not seeing a connection between using 2's complement addition and creating a program in machine language (using the GUI to input code) that subtracts arbitrary numbers (subtract A2 from A1 and store the value in A3). A1, A2, and A3 are all random memory locations. I'm pretty sure he explained that it didn't matter what the values were and that they could be variables. I interpreted his instructions as:
1. Place (X) value in a specified memory location.
2. Create a list of instructions that will subtract the values.

I'm now positive that 2's complement comes into play at some point, but I'm not understanding where the math behind it comes into play with this exercise.

The only idea I have is that maybe we're expected to place our own values in the specified memory locations. I wouldn't think that we would be expected to write introductory level programs with that much abstraction. I say that because question 2 reads:

Write a program that will multiply the value in memory cell A4 by 2 and place the result in cell A5. Start your program in memory cell 30. You can assume that the content of cell A4 is a positive integer expressed in 2’s complement notation.

Today we were also told that these programs did not actually have to compile or run, which added to my confusion.
 
  • #12
The binary form of a positive integer doesn't change when you "express it in 2s complement notation", just be sure to provide leading zeros. It's only negative integers that need a procedure to change them in 2s complement notation so you can drop the negative sign.

Let me express things this way:

When working with 8 bit registers this potentially allows you to store integers 0..255. Obviously, these are all positive (or unsigned, choose whatever word you prefer). But if you need to store negative numbers, you are SOOL. There are no spare bits to label a "sign bit"! Who is going to buy a computer that can't accommodate negative numbers?

So, instead of storing numbers 0-255 and never being able to handle negatives, we can instead use that same sized register to represent positive numbers 0..127 and negative numbers -128..-1

It turns out that if the left-most bit is 1, then the number represented is a negative. But don't regard it as a simple "sign bit". The reality is that arithmetic processing can be kept very straightforward if the negative number before it it stored is converted to a form known as 2s complement form.

When you feed numbers to the processor, it sums them blindly as though they are all positive. It is up to the programmer to test the result and if the MSB =1 conclude that the number represented is negative and process it accordingly before presenting it to the user.

This is the beauty of 2s complement notation, the processor treats all addition as though it were addition of positive numbers, and it doesn't need to know how to do subtraction of integers. You, as the programmer, preprocess negative numbers before storing them in a register, and postprocess them before printing them out. The CPU believes it is performing arithmetic on all positive numbers so can be designed with a simpler command set.
 
  • #13
In case you're curious, another way of dealing with negative numbers is to use 1's complement. For 1's complement math, a 1's complement is done by simply toggling all the bits from 0 to 1 or 1 to 0. Note that the 1's complement of zero produces all 1 bits, for an 8 bit number positive zero is 00000000, and negative zero is 11111111.

When a 1's complement machine does addition, if there's a carry, it increments the result. This means it's always treating numbers as if they are 1's complement signed numbers, unless there's an option to disable incrementing if there's a carry.

I'm not aware of any modern processors that use 1's complement math, but it helps explain why the math is called 2's complement math, to explain that it's not 1's complement math.
 

1. What is machine language?

Machine language is the lowest level of programming language that is directly understood by a computer. It consists of binary code (0s and 1s) that represents the basic instructions and data that the computer can execute.

2. How does a computer perform addition and subtraction in machine language?

A computer performs addition and subtraction in machine language by using arithmetic logic unit (ALU) instructions. These instructions involve moving data between registers, performing the desired operation, and storing the result in a designated register.

3. Can a computer perform complex calculations using only addition and subtraction in machine language?

Yes, a computer can perform complex calculations using only addition and subtraction in machine language. This is because all other mathematical operations (such as multiplication and division) can be broken down into a series of addition and subtraction operations.

4. Are there different instructions for addition and subtraction in machine language?

Yes, there are separate instructions for addition and subtraction in machine language. These instructions are called "add" and "subtract" and they have different binary codes that the computer interprets to perform the desired operation.

5. How is the result of an addition or subtraction operation stored in machine language?

The result of an addition or subtraction operation is stored in a designated register in machine language. This register is a small storage area within the computer's memory that can hold a specific amount of data. The result can also be stored in a memory location if specified by the programmer.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Programming and Computer Science
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top