Niehm
Niehm

Reputation: 143

Assembly: Outputting variables, OS dependent?, Executing

I am trying to learn some Assembly after we talked about it a little in class. I am trying to translate a basic java program to find a Fibonacci number into assembly and I think I got the logic right, but my friend tells me that it might not work on my Windows 7 64bits computer because ASM is different depending on your OS ( :O Is he serious? )

Here is my code:

SECTION .data   

Var2: resd 3
Var5: resd 4
Var1: db 10
Var3: db 1
Var4: db 1

SECTION .text   
global main
main:

MOV Var2,0

Loop1:
  ADD Var2,1
  MOV Var5,Var3
  ADD Var3,Var4
  MOV Var4,Var5
  CMP Var2,Var1
  JE Fin
  JP Loop1

Fin:
Put printing in here.

2/ Is there something similar to System.out.print in ASM?

3/ How exactly do I execut it once I figure that out? I got RadASM but I can't seem to run my code through it. How do I execute the ASM program once done ?

Thank you.

Upvotes: 0

Views: 144

Answers (1)

nrz
nrz

Reputation: 10560

  1. Almost every binary executable program is dependent on the OS, independently of the programming language it's written in. Programs written in assembly are no exception. Don't confuse bytecode (eg. Java) with native processor code (eg. x86-64). And of course you have to follow the syntax of the programming language you use. Your program will not assemble on most assemblers due to the syntax errors related to memory addressing.

  2. In protected mode OSes such as Windows 7 you have to use your OS API for printing.

  3. You execute a program written in assembly as any other binary executable. First you write the code, then you assemble and link it, and then execute. Just like any other binary executable. The difference with higher-level compiled languages is that assembling is a lot simpler task for the assembler (the program) to do than compiling of higher-level source code for the compiler.

In x86-64 assembly you can't move from memory address directly to another. You have to first move the one of the general registers of the processor, and then from that register to another memory address. However, there are instructions that move from memory to memory too, such as movsb, movsw, movsd and movsq, but to get started with, it's easier to learn them later.

It's not clear what you want the size of the variables to be, as you use db for some, and resd for others. If you want that all Var1, Var2, Var3, Var4, Var5 to be bytes, then it should be for example:

Var2: db 3
Var5: db 4
Var1: db 10
Var3: db 1
Var4: db 1

And if you want them to be for example dwords, replace every db with dd. resd means reserve dwords, to reserve bytes you should use resb.

You must know the size of the operands in assembly. Assembler does not make the decisions for you.

Assuming all variables above are bytes (db, 8 bits each), the loop could be something like (NASM/YASM syntax):

Loop1:
    ADD [Var2],byte 1

    MOV al,[Var3]
    MOV [Var5],al

    mov al,Var4
    ADD Var3,al

    mov al,[Var5]
    MOV Var4,al

    mov al,[Var1]
    CMP Var2,al
    JE Fin
    JP Loop1

And if you want them to be dwords (dd, 32 bits each):

Loop1:
    ADD [Var2],dword 1

    MOV eax,[Var3]
    MOV [Var5],eax

    mov eax,Var4
    ADD Var3,eax

    mov eax,[Var5]
    MOV Var4,eax

    mov eax,[Var1]
    CMP Var2,eax
    JE Fin
    JP Loop1

Upvotes: 1

Related Questions