1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Addition and subtraction in machine language

  1. Jan 24, 2012 #1
    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: Jan 24, 2012
  2. jcsd
  3. Jan 24, 2012 #2

    Mark44

    Staff: Mentor

    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 (Text):

             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 (Text):
     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.
     
  4. Jan 24, 2012 #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.

    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 (Text):
    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!
     
  5. Jan 24, 2012 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Jan 24, 2012 #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.
     
  7. Jan 25, 2012 #6

    NascentOxygen

    User Avatar

    Staff: Mentor

    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.
     
  8. Jan 25, 2012 #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.
     
  9. Jan 25, 2012 #8

    rcgldr

    User Avatar
    Homework Helper

    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 (Text):

        xor R0,R1
        xor R1,R0
        xor R0,R1
     
     
  10. Jan 25, 2012 #9

    NascentOxygen

    User Avatar

    Staff: Mentor

    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:
    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 (Text):
            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: Jan 25, 2012
  11. Jan 26, 2012 #10
    I'm just dropping in to 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!
     
  12. Jan 26, 2012 #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 in to 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:

    Today we were also told that these programs did not actually have to compile or run, which added to my confusion.
     
  13. Jan 26, 2012 #12

    NascentOxygen

    User Avatar

    Staff: Mentor

    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.
     
  14. Jan 26, 2012 #13

    rcgldr

    User Avatar
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Addition and subtraction in machine language
Loading...