Copy memory zone

Posted on 2006-06-23
Last Modified: 2008-01-09
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
Question by:Ameerh24
  • 4
  • 2
  • 2
  • +1

Author Comment

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?

Accepted Solution

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
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
                             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

Author Comment

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 ..
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 16

Expert Comment

ID: 16975437
Hi Ameerh24,

You could try ADD R3,0

LVL 18

Expert Comment

ID: 16976144

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.

LVL 18

Assisted Solution

JoseParrot 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.
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...

Expert Comment

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.


LVL 18

Expert Comment

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.

LVL 18

Expert Comment

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.

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show semaphore name in Pascal 12 642
Help! Solving Phase 4 of my binary bomb 9 4,390
Using MSBuild to compile asm files in VS2010 3 1,637
What are the %fs and %gs registers? 3 1,231
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.

803 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