Solved

Copy memory zone

Posted on 2006-06-23
9
514 Views
Last Modified: 2008-01-09
hi,
suppose I have the following instruction set of a CPU :

Operation                                                       Binary Code
NOP                                                                  0000 0000
JMP  HHLL                                                        0111 0000 – llll llll – hhhh hhhh  <----(l for low, h for high)
JZ HHLL                                                                  0111 0001 – llll llll – hhhh hhhh
JC HHLL                                                                  0111 0010 – llll llll – hhhh hhhh
JMP RX0                                                                 0111 0011
ST R0,RXn                                                        0111 10nn
LD R0,RXn                                                         0111 11nn
ST Rn HHLL                                                        0100 0nnn – llll llll – hhhh hhhh
LD Rn HHLL                                                        0100 0nnn – llll llll – hhhh hhhh
MV Rn, arg#                                                       0101 0nnn – aaaa aaaa <-------(arg# means that this is immediate addressing mode argument)
DEC Rn                                                                  0101 1nnn
INC Rn                                                                   0110 0nnn
NOT Rn                                                                  0110 1nnn
ADD Rn, Rm                                                       100n nmmm
SUB Rn, Rm                                                       101n nmmm
AND Rn, Rm                                                       110n nmmm
SWP Rn, Rm                                                      111n nmmm
MV Rn,Rm                                                        00nn nmmm

Where the CPU has 8 registers (R0,R1,R2,...,R7) and eaxh register is 8 bit  and RXn means the combination of Rn and Rn+1
(For example RX0means the combination of R0 and R1).


what is needed is to Write an assembly program to copy a memory zone starting from memory address stored in 0100h (indirect addressing mode) to another zone starting from memory address stored in 0102h (indirect addressing mode) and stops when it found the value 00h.Suppose that the zones are distinct.

Note: The program in hexadecimal code knowing that it starts from address 0200h.

so any help would be greatly appreciated
0
Comment
Question by:Ameerh24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 

Author Comment

by:Ameerh24
ID: 16968749
I tried this :
0000                      LD R0 Low 0101
0001                      LD R1 High 0100
0010                      LD R1 Low 0103
0011                      LD R2 High 0102
0100                      LD R3, R0
0101                      AND R3,00
0110                      JZ 1011
0111                      ST R2, R3
1000                      INC R1
1001                      INC R2
1010                      JMP 0100
1011                      NOP

so is that right?
0
 
LVL 3

Accepted Solution

by:
PeterdLo earned 250 total points
ID: 16969528
Hi Ameerh24,

basically your coding is alright. You had following problems.
1) Do not use AND R3,00, it will always get result 0.
2) When set up source pointer and destination pointer, try to use separate set. For example, RX0 for source ptr and RX2 for destination ptr.

Here is my hint. Please fix up the addresses where those instructions reside.

0000                      LD R0 Low 0101
0001                      LD R1 High 0100
0010                      LD R2 Low 0103
0011                      LD R3 High 0102
copy_next:
0100                      LD R4, RX0
                             JZ done     ;quit when source char = 0
0101                      ST R4, RX2  ;otherwise, store via destination pointer
0110                      INC R0
                             JZ  bump_r1
bump_target_ptr:
                             INC R2
                             JZ  bump_r3
                             JMP copy_next
bump_r1:               INC R1
                             JMP bump_target_ptr
bump_r3:               INC R3
                             JMP copy_next
done:                     NOP
0
 

Author Comment

by:Ameerh24
ID: 16974796
Hi PeterdLo,
first of all thank you very much because of your corrections..
You are right that I should not use AND R3,00 (because it's always resulting ZERO)..
but, The problem is that there is no compare (CMP) instruction in this CPU instuction set..(and the program must stop copying when it found the value 00h)
so I should compare the value in memory if it's equal to zero.
so what should I so??
oops..can I use AND R3,11??
so, the result will ditect if the value of R3 is zero ..
0
Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16975437
Hi Ameerh24,

You could try ADD R3,0

Paul
0
 
LVL 18

Expert Comment

by:Jose Parrot
ID: 16976144
Hi,

Think on
                      AND R3,R3
as a checking for R3 to be zero.
The advantage of this test is to not destroy R3 content, just change the flag.

So, the code can be changed to
   :
0100                      LD R3, R0
0101                      AND R3,R3
0110                      JZ 1011
   :

Also, a conditional jump as   JZ address   should be made after a boolean or arithmetic operation. A simple LD doesn't change the flags. For clarity, we use to test (AND, INC, ADD) just before the conditional jump.

Jose
0
 
LVL 18

Assisted Solution

by:Jose Parrot
Jose Parrot earned 250 total points
ID: 16976496
I forgot to include the complete suggestion:

0000                      LD R0 0101 0000  <---- the source. Assuming HIGH = HHHH = 0000
0001                      LD R1 0100 0000   <---- Direct addressing is LLLL HHHH
0010                      LD R2 0103 0000  <--- use R2, as R1 is already used to HHHH source
0011                      LD R3 0102 0000  <--- use R3. The pair R2,R3 used to poit to destin
0100   loop:            LD R4 RX0  <--- register R4 availble to data. Indirect addressing is pair R0,R1, so RX0
0101                      AND R4,R4          <--- as we don't have COMPARE, and R4 with itself
0110                      JZ 1011 0000  <--- if zero,  finish move loop. Direct addressing is LLLL HHHH
0111                      ST R4 RX2   <--- data in R4 is stored at address RX2
1000                      INC R0         <--- see note 1
1001                      INC R2
1010                      JMP 0100 0000 <--- Direct addressing! jump to loop
1011                      NOP

note 1
If we use only INC R0, we advance to next address, but limited to a 256 addresses range. If the program should allow larger range, on must verify the register R0. If it turned to zero then increment R1 by one.
Exemple:
1000                      INC R0        
1001                      AND R0, R0 <--- zero?
1010                      JZ incr1      <--- if zero, R1 must be incremented
                                                     to pair RX0 point correctly to next address
1011                      JMP testr2        
1001     incr1:         INC R1
1011     testr2:        AND R2,R2  <--- do the same test in pair RX2
1100                      JZ incr3       <--- if zero, R3 must be incremented
                                                      to pair RX2 point correctly to next address  
1101                      JMP loop     <--- if not, loop again        
1110     incr3:         INC R3
1111                      JMP loop

Attention: destin address (pair R2,R3) must point to regions not in the program, that will be auto-destroyed if that happens...
                     
Jose
0
 
LVL 3

Expert Comment

by:PeterdLo
ID: 16977459
Hi, Ameerh24,

Most Assembly Languages will automatically set the Z (zero flag) when it loads the byte data into the register, for example, LD R4,RX0 (load data
thru indirect pointer/address). Therefore, you do not need the Comparison instructions. Unless, your language does not have this feature.

Same thing for the incremental instruction, for example, INC R0. When its content wraps around from 0xff (decimal 255) to 0x00, the Z flag will be set to indicate the content of R0 is zero.

Otherwise, you need to follow other experts advices to test Z flag different ways.

Peter

0
 
LVL 18

Expert Comment

by:Jose Parrot
ID: 16989160
Hi Ahmeer24,
As a matter of clarifying, change in the ZERO flag occurs after logical (OR, AND, XOR) and math (ADD, SUB, MUL, DIV, INC, DEC) operations.
This is an attribute of the CPU, not a feature of the assembler or language compiler.
There are no actions on the ZERO flag when the CPU run operations like LOAD, STORE and MOVE.
This applies to all the Intel family (8080, 8085, 8088, 8086, 80186, 80286, 80386, 80486 and Pentium I to 4), AMD, Zilog (Z80, Z8000), Motorola.

About INC and DEC actions on the ZERO flag, Peter is correct.

BTW the CPU in the case of current question is very primitive and seems to be a theoretical one.
The CMP A,B action is only a SUB A,B action that doesn't change the register content, only the ZERO flag if the result is zero.

Jose
0
 
LVL 18

Expert Comment

by:Jose Parrot
ID: 16999061
Ooops, correcting:
The CMP A,B action is only a SUB A,B action that doesn't change the register content, only the ZERO flag, which will be TRUE if the result is zero and FALSE otherwise.
Jose
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A look at what happened in the Verizon cloud breach.
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question