Difference between revisions of "Assembly Language Twos Complement"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "You can use a Bitwise NOT to flip all of the bits in a register, ie a binary digit of 0 becomes 1 and so on. We can use this to write a program to convert numbers to Two's Com...")
 
(Explanation)
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
==Program==
 
==Program==
      inp r0,2
+
mov r0, #20
      mvn r1,r0
+
mvn r1,r0
      add r1,r1,#1
+
add r1,r1,#1
      out r1,
 
  
 
==Explanation==
 
==Explanation==
Line 1 will get a value from the keyboard, and store it in Register 0.
+
Line 1 will get a value to use, and store it in Register 0.
  
 
Line 2 will perform a Bitwise NOT on the value in Register 0, and store it in Register 1.
 
Line 2 will perform a Bitwise NOT on the value in Register 0, and store it in Register 1.
Line 14: Line 13:
 
Line 3 will Add #1 to the value in Register 1, and store it in Register 1.
 
Line 3 will Add #1 to the value in Register 1, and store it in Register 1.
  
Line 4 will output the value in Register 1 to the screen.
+
The answer will be stored in Register 1.

Latest revision as of 13:19, 21 January 2020

You can use a Bitwise NOT to flip all of the bits in a register, ie a binary digit of 0 becomes 1 and so on. We can use this to write a program to convert numbers to Two's Complement:

Program

mov r0, #20
mvn r1,r0
add r1,r1,#1

Explanation

Line 1 will get a value to use, and store it in Register 0.

Line 2 will perform a Bitwise NOT on the value in Register 0, and store it in Register 1.

Line 3 will Add #1 to the value in Register 1, and store it in Register 1.

The answer will be stored in Register 1.