# need assembly language code



## jolly1808 (Mar 26, 2006)

hi there, i have this question, yes it is work i have to do, so dont bother wasting your/mine time saying do it by your self, thanks.

i am told the high-level language:
For I <-- 1 To 10 Do
A <--- A + 1;
i want to write this int he assembly language equivalent of this.

would be greatly appreciated if you could help, 
thanks alot


----------



## lotuseclat79 (Sep 12, 2003)

jolly1808 said:


> hi there, i have this question, yes it is work i have to do, so dont bother wasting your/mine time saying do it by your self, thanks.
> 
> i am told the high-level language:
> For I <-- 1 To 10 Do
> ...


Hi jolly1808,

If I is your index, then where is I used in A <--- A + 1;? If you intend A to be an array then you need to index A with I to initialize the array if that is your intention.
In assembler language, registers or memory can be used to implement an array.

A simple way to understand the assembler code, is to write it in C, and after the C program is verified to work, then recompile and toggle the compiler switch that emits assembler language code to see how it is done by the C compiler, or choose another language that emits assembler, then adapt it for your use.

-- Tom


----------



## dquigley (Apr 17, 2006)

What processor for Intel 8088 it would be:


```
mov   cx,10  ; cx is typically used as a counter register in intel

lable_1:
   ; based on your pseudo code
   inc   ax        ;  A<--A + 1

   inc   cx
   cmp   cx, 0
   jg   lable_1
```


----------



## jolly1808 (Mar 26, 2006)

thanks both of you for your reply, unfortinatly, i cant really understand what you are saying.
i have just started to learn how to write with assembly language (like yesterday), and according to my guide, i should be able to write the program

for i <-- 1 to 10
a <-- a + 1;

in assembly language.

so far, the coomands i have learnt is:
LD, ST, MOVE, ADC, SBC, INC, DEC, NOT, ADC, and i have read on to read about CLC, RTS and ASL.

if someone or dquigley himself and re-write what dquigley wrote using the commands i have learnt so i can follow what you are doing would be really great.

thanks for any help you can give me,,, 1 day i will understand this


----------



## dquigley (Apr 17, 2006)

To help you I need to know what processor you are writing this code for. From the instructions you list, it appears like an ARM or AVL RISC processor. 

Best,
Dan


----------



## jolly1808 (Mar 26, 2006)

dquigley said:


> To help you I need to know what processor you are writing this code for. From the instructions you list, it appears like an ARM or AVL RISC processor.
> 
> Best,
> Dan


thanks again for your help, but i must stress that i am not writting this for any processor, im am just simply learning about assembly language.
EDIT: i am told that these codes are used for a typical computer system.


----------



## dquigley (Apr 17, 2006)

Every processor family has unique instructions sets and syntax. "Assembly Language" is not standardized like higher level languages like C or C++. When you write in "Assembly" you write to a specific processor/family. Like Intel 80xxx or TI2430 or Motorola 6502, etc. Assemblers take text representations of instructions and assemble them into machine readable code. They are typically designed and developed by the processor manufacturer and vary in features and capabilities.

Somewhere in the materials you are reading there should be something that tells you what processor those instructions are for.

Best,
Dan


----------



## dquigley (Apr 17, 2006)

If this is a "generic" assembly language class, then I will need to know the description of the processor and each instruction. I don't see any in the list you provide that stands out as a branch/test or loop instruction. You will need one or more of those to code the FOR loop.

Best,
Dan


----------



## jolly1808 (Mar 26, 2006)

unfortinatly i can not tell you what proccessor it is for, all i am told is that these instructions are a set of a typical computer.

i really appericate your help, and lasting this long with me,, 
so far, this is what i have come up with:

let i = B
----------------------------------------------------------------------------------------------------
LD B
LD A #0
IN B
CMP B #10 (label = label_1)
JMP label_stop
HALT (label = label_stop)

yeah so,, i really am stuck 
this code above prob makes no sense, but thought i would write it to see if you can kinda get from that what i mean/ment to do,
again, thanks so much for trying to help.

EDIT
perhaps this would help tough, i am givin as an example:
Q: write assembly language of this:
A<-- 0;
Repeat
A<-- A + 1;
Until A = 99

A:
LD A, #0
ST A, LOCA
LD A, LOCA (label = REPEAT)
INC A
STA, LOCA
CMP A, #99
JPNZ REPEAT
HALT

EDIT2:
LD A, memref --> loads the info identified by memref into the register A
ST A, memref --> stores the contents of reg A into the location identified by memref
INC A --> adds 1 to the value in reg A
CMP A, memref --> compares the contents of reg A with the data identified by memref, and sets the Zero flag if they are equal
JP coderef --> jmps to the code location labelled coderef
JPZ coderef --> as for JP, but the jump only happens if the zero flag is set
JPNZ --> as for the JPZ, but the jump only happensif the zero flag is set
HALT --> stop


----------



## dquigley (Apr 17, 2006)

From the looks of things, the basics haven't clicked with you yet. For a simple computer think of four basic things:

1) Processor Instructions - perform actions on registers and/or memory

2) Processor Registers - temporary holding places for values

3) Processor Flags - a special register (on some chip architectures) that holds status/state values

4) Memory - holds both code (instructions) and values (data)

A simple loop that does not use memory (just registers) is:


```
LD A, #0   ; load register A with the value 0
LD B, #10  ; load register B with the value 10

:LOOP1     ; a label - this tells the assembler to remember the 
           ; location (in memory) of the next instruction ("INC A" in this case)

INC A      ; increment register A by 1
DEC B      ; decrement register B by 1

; depending on the processor this next instruction may not be required
; because on some processors the zero flag is set when any register is
; decremented or incremented to the value zero

CMP B, #0  ; compare the value in register B with 0 (this sets or clears a flag 
           ; usually in a special flags register)
JPNZ LOOP1 ; if the zero flag is not set (off) jump to label LOOP1 (location of "INC A")
HALT
```


----------



## jolly1808 (Mar 26, 2006)

hey, thanks this really helps,

so, for my problem:
For I = 1 to 10 do (let I = B)
a = a + 1 

a possible way to answer this is:

LD A, #0
LD B, x ; x is my input
:loop1
cmp B, #10
INC A
INC B
JPNZ loop1 
HALT

this seems quite simple.... would this be correct ?

ACTAULLY, this would go on forever, i will change it to:
LD A, #x
LD B, 0 ; x is my input
:loop1
INC A
INC B
cmp A, #10
JPNZ loop1 
HALT


----------



## dquigley (Apr 17, 2006)

```
LD A, #x
LD B, 1 ; x is my input  B needs to start at 1 per your problem  FOR I = 1 to 10 (let I=B)
:loop1
INC A
INC B
cmp B, #10 ; need to compare B
JPNZ loop1 
HALT
```


----------



## jolly1808 (Mar 26, 2006)

dquigley said:


> ```
> LD A, #x
> LD B, 1 ; x is my input  B needs to start at 1 per your problem  FOR I = 1 to 10 (let I=B)
> :loop1
> ...


i cant thank you enough for helping me here, not only did u answer my question, but now i completly understand how you did it.
last thing, is it only values in the accumlator that can be compared, or can any value inany registar be compared ?


----------



## dquigley (Apr 17, 2006)

You're welcome.

Most modern processors allow you to compare all registers and memory etc. For example in an Intel segmented model the following code will compare two 16 bit values, each in memory:


```
cmp ds:[si], es:[di]  ; [] brackets around a register instruct the processor
                      ; to reference a location in memory not the value of the register
```
This is a pretty good site about Assembly.
The Art of Assembly Language Programming

Good Luck,
Dan


----------



## lotuseclat79 (Sep 12, 2003)

Here is another good link about assembly language:
http://en.wikipedia.org/wiki/Assembly_language

-- Tom


----------



## jolly1808 (Mar 26, 2006)

thanks both,, again 

and yeah,, first thing i did was read wiki


----------

