Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays - Assembly x86 1

Status
Not open for further replies.

TexFlex

Programmer
Feb 12, 2008
9
0
0
Can anyone suggest a book or a good website so that I can understand how arrays work, and how to create them. I am a visual learner, and I need a book or website that uses visuals to demonstrate the process.

Thanks for any help you can give me.

 
I'm not sure you'll get to see much help in too many venues. Anyway, there's not going to be too much "visual" to get in anything (whatever you mean by that anyway?) anyway. Or much clear and good - mostly just examples more than anything that is clearly explained.

Anyhow, here's something simple, hopefully you can understand it. I'm trying to learn from some things I have here as well and can't say I'm acing anything for right now.

Code:
program count;
  { takes table, puts number in table that is index * 2,
    then prints it }
  var
    mytable: array[0..9] of byte;
    i: integer;

  procedure work; assembler;
    label
      loop_start1;
    asm
      MOV BX, Offset MyTable  { BX := offset of MyTable }
      MOV CX, 0               { CX := 0 }
      Loop_Start1:
      MOV AX, CX              { AX := CX }
      ADD AX, CX              { AX := AX + CX }
      MOV [BX], AX            { MyTable[i] := AX }
      INC CX                  { CX := CX + 1 }
      INC BX                  { BX := BX + 1 }
      CMP CX, 10              { is CX = 10? }
      JLE Loop_Start1         { jump to loop if CX <= 10 }
    end;

  begin
    work;
    for i := 0 to 9 do
      writeln(i, ': ', mytable[i]);
  end.

----------
Measurement is not management.
 
Oh, and the target of the source I posted is Turbo Pascal (16-bit ASM there).

----------
Measurement is not management.
 
Glenn9999,

Thank you.

Your post was very helpful.
 
Found a little error:

Code:
CMP CX, 10              { is CX = 10? }

This line should be:

Code:
CMP CX, 9              { is CX = 9? }

The error is shown by the loop taking the addressing past the table into the next variable. You can see that by writing i before the loop and having it be 20 (10*2), which makes sense when you look at i in binary form.

----------
Measurement is not management.
 
Glenn9999,

Thank you for the correction. Can you help me convert an If statement to assembly language.

The if statement is:

if (value > 100) and (count <= 10)
then
subtract 5*count from value;
end if;

This is what I have:

cmp value, 100
jne
cmp ecx, 10
jl
mov edx

This is where I left off. Can you help me with this
 
Turbo Pascal (16-bit ASM) again. On the jump comparison you have to give it a place to jump to, so I negated the logic. The logic as it is does short-circuit evaluation as well, so it'll change a bit if you want both values evaluated regardless.

Code:
program ifstmt;
  var
    count, value: integer;
    orig_value: integer;

  procedure work_asm; assembler;
    label
      end_jump;
    asm
      CMP    value, 100  { compare value with 100 }
      JLE    end_jump    { go to end if value <= 100 }
      CMP    count, 10   { compare count with 10 }
      JG     end_jump    { go to end if count > 10 }

      MOV    AX, count   { ax := count }
      MOV    BX, value   { bx := value }
      MOV    CX, 5       { cx := 5 - can not do operation on constant }
      MUL    CX          { AX := ax * cx }
      SUB    BX, AX      { BX := BX - AX }
      MOV    Value, BX   { value := BX }
      end_jump:
    end;

  procedure pas_work;
    begin
      if (value > 100) and (count <= 10) then
        value := value - (5*count);
    end;

  begin
    writeln('Count <= 10 and value > 100 for value to change');
    writeln;
    write('Value: ');
    readln(value);
    write('Count: ');
    readln(count);
    orig_value := value;
    pas_work;
    writeln('PAS Value is now: ', value);
    value := orig_value;
    work_asm;
    writeln('ASM Value is now: ', value);
  end.

Hope this helps. And hopefully for learning a few things myself in writing this that there aren't any bugs in it that I haven't already corrected.

Glenn

----------
Measurement is not management.
 
Glenn,

Thank you again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top