Author Topic: from NASM to MASM  (Read 10530 times)

Offline Eght

  • Jr. Member
  • *
  • Posts: 3
from NASM to MASM
« on: January 03, 2017, 06:41:21 PM »
Hello guys.
My task is to change code from MASM to NASM. I changed the code 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. Still don't know how to change to nasm printf.

This is sieve of eratosthenes

 
Code: [Select]
; 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 :
Code: [Select]
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


Just now I changed
Code: [Select]
  array1: dd arraySize dup(0) ; initialize arrayto
Code: [Select]
array1: times arraySize dd 0 Is this valid syntax? It leads to following errors

Code: [Select]
primes.asm:40: error: operation size not specified
primes.asm:40: error: operation size not specified
primes.asm:49: error: operation size not specified
primes.asm:60: error: operation size not specified
primes.asm:67: error: operation size not specified
primes.asm:86: error: operation size not specified

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: from NASM to MASM
« Reply #1 on: January 03, 2017, 08:07:16 PM »
Ah, you already started a new topic - good!

Your syntax for "times" looks good. Masm "remembers" that you said "dd", but Nasm has amnesia. You will have to tell Nasm:
Code: [Select]
cmp [array1 + 4 * ecx], dword -1
etc.

Good start. More later...

Best,
Frank