News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

from NASM to MASM

Started by Eght, January 03, 2017, 11:36:41 PM

Previous topic - Next topic

Eght

Hello guys.
My task is to change code from MASM to NASM. I changed a little bit but it is still not enough. The most important part is the print section. I've seen the documentation about nasm and masn differences, but everything is like a black magic for me. Can you guys give me some advices?

This is sieve of eratosthenes

; size of array to hold the prime candidates
; (arraySize - 1) is the max prime candidate.

SECTION .data


    arraySize EQU 10000
    array1: dd arraySize dup(0) ; initialize array

    .code

SECTION .text

extern printf
extern scanf

global main

main:

    cls

    xor ecx, ecx ; zeroing, is slightly faster than MOV reg, 0

   fillArrayLoop:
  mov eax, ecx ; Move counter value to eax
  add eax, 2 ; The sieve starts at 2, therefore add 2 to the loop counter
  mov [array1+4*ecx], eax ; Insert value into array
  inc ecx ; Increment counter
  cmp ecx, arraySize ;
  jb fillArrayLoop ; jump below, Jump to loop start is we are not finished



    xor ecx, ecx ; Zero out counter
    loop1: ; This is out outer loop
mov ebx, ecx ; Prepare ebx to be used af counter in innerloop (loop2)
inc ebx ; Inner loop starts at ecx + 1
  cmp [array1+4*ecx], -1 ; Value discarded? So is value -1?
  jne loop2 ; if not jump to inner loop(loop2)
  resume1: ; Resume point
inc ecx ; Increment out primary counter
cmp ecx, arraySize ; Are we done?
jb loop1 ; If not jump to loop start.
jmp theEnd ; All done!

loop2: ; Inner loop
cmp [array1+4*ebx], -1 ; Value discarded?
jne loop3 ; if nor jump to loop3
resume2: ; Resumt point
inc ebx ; Increment inner loop counter
cmp ebx, arraySize ; Are we done?
jb loop2 ; if not go to loop start.
jmp resume1 ; Loop2 dont, return to loop1.
loop3: ; Here we will test if it is a prime...
xor edx, edx ; Zero out edx
xor eax, eax ; Zero out eax
mov eax, [array1+4*ebx] ; Place the number we want to divite in eax
div [array1+4*ecx] ; Divide the numbe in eax with the value from outer loop
cmp edx, 0 ; Check edx for remainder
je nukeIt ; If we have no remainder it the number in eax is not a prime
; therefore we nuke it. (set it to -1)
resume3: ; Resume point
jmp resume2 ; Done, jump to resume point in loop2
    nukeIt:
    mov [array1+4*ebx], -1 ; Not a prime, set it to -1
    jmp resume3 ; Jump to resume point

    printArrayElement: ; Just printing all the primes.
    push ecx ; Preserve ecx
    print str$([array1+4*ecx]), 10, 13 ; Using the masm32 print macro
    inc ebx ; Incrementing a counter used to store the number of primes.
    pop ecx ; Callback the preserved ecx
    jmp resumePrintArray ; Jump to resumepoint in print array.


   theEnd: ; exit point


    xor ecx, ecx ; Zero out.
    xor ebx, ebx ; Zero out.

    printArray:
    cmp [array1+4*ecx], -1 ; Check if we have a prime.
    jne printArrayElement ; If we have jump to printArrayElement.
    resumePrintArray: ; Resume point
    inc ecx ; Incrementing
    cmp ecx, arraySize ; Are we done?
    jb printArray ; If not jump to printArray...

print "Number of primes below "
print str$(arraySize)
print ": "
print str$(ebx), 10, 13

    ret



and console output :
nasm -f elf primes.asm && gcc -m32 -o primes primes.o
primes.asm:9: error: comma expected after operand 1
primes.asm:71: error: parser: instruction expected
primes.asm:91: error: symbol `print' redefined
primes.asm:91: error: parser: instruction expected
primes.asm:92: error: symbol `print' redefined
primes.asm:92: error: parser: instruction expected
primes.asm:93: error: symbol `print' redefined
primes.asm:93: error: parser: instruction expected
primes.asm:94: error: symbol `print' redefined
primes.asm:94: error: parser: instruction expected

jj2007

cls
print
str$

These are Masm32 macros. You will have to find the NASM equivalent for those. Have you tried that in a NASM forum?

raymond

I have two comments.

i) I don't know who is the author of the 'masm' code and I don't know either who titled it as sieve of eratosthenes. That title does NOT match the code.

ii) The provided masm code is very difficult to read and follow. My suggestion is to list all labels directly against the left margin and then all code instructions a few spaces away from the left margin. Additional indentations within the code (such as for .if/.endif or similar directives) may also be considered.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html