3 Replies - 273 Views - Last Post: 06 November 2019 - 12:15 AM

#1 SegNotMyFault   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 19

New to NASM and would like some help :)

Posted 05 November 2019 - 11:26 PM

Hello there fellow programers. I am really new to Nasm, I got into it because I know some MIPS and wanted to learn a new flavour of assembly. But to my surprise it has set up a goal to make my life very hard ahahah. Anywho I have written a program I translated from perl to C++ and then now I want to translate it to NASM assembly. I have all of it translated from the C++ code to the NASM code with all the lables and call I think it needs however when I run it it only prints the first two lines and then it thorws SegFault error type 8. I am really not able to track down where the error is coming from nor how to fix it. I have ran gbd on my C++ code but the NASM code I get from it is very far from the one I have written. I would really apreciate if anybody could help me see what am I doing wrong and how to fix it. Perhaps the code is not wrong but maybe the calling convention. Thank you all very much in advance!

Expected output

ORDERS OF THE DAY:
=========================
Order: Groceri1 Pick up Apples
Order: Market1 buy bread
Order: Warehouse1 sell asparagus
Order: Groceri2 deliver rice
EPSILON


NASM VERSION

 section .text
        global  _start



rand:                   ;result in eax
        rdtsc
        ret
print:
        mov     ebx, 1
        mov     eax, 4
        int     0x80
        ret

pick_actor: 
        ;call rand
        rdtsc
        mov ecx, 6
        div ecx    ; remainder is in edx
        
        mov eax, actors
        mov ecx, len1
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len11
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        
        
        call print
        ret
pick_action:
        rdtsc ;call rand
        mov ecx, 4
        div ecx    ; remainder is in edx
        
        mov eax, actions
        mov ecx, len2
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len22
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        call print
        ret
pick_acted:
        ;call rand
        rdtsc
        mov ecx, 7
        div ecx    ; remainder is in edx
        
        mov eax, acted
        mov ecx, len3
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len33
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        ret
order: 
        mov ecx, orderstr
        mov edx, len7
        call print
        
        call pick_actor
        call pick_action
        call pick_acted
        
        mov ecx, newline
        mov edx, 1
        call print
        ret



_start: 






          mov ecx, orders_of
          mov edx, len4
          call print
          
          mov ecx, separator
          mov edx, len5
          call print
          
          
loop1:                
         call rand  ; eax
         mov edx, 7
         mov ecx, 100
         div ecx    ; eax:edx
         sub ecx, edx
         jz loop2
         call order
         call loop1
loop2:  
         mov ecx, epsilon
         mov edx, len6
         call print
          
         call EXIT
         
EXIT:
         int 80h


    ;mov     ecx, msg
    ;mov     edx, len


        section   .data

actors:     db "Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"
len1:       db  0,  7,  14,  24,  34, 41
len11:      db  7,  7,  10,  10,   7,  7

actions:    db "buy", "sell", "deliver", "pick up"
len2:       db 0,3,7,14,21
len22:      db 3,4,7,7

acted:      db "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery"   
len3:       db 0,4,13,20,26,33,38,44
len33:       db 4,9,7,6,7,5,6

orders_of:  db "Orders of the day:",0xA
len4 equ $ - orders_of

separator:  db "====================",0xA
len5 equ $ - separator

epsilon:  db "EPSILON",0xA
len6 equ $ - epsilon

orderstr: db "ORDER : "
len7 equ $ - orderstr

newline:         db 0xA

SECTION .bss
    msg1: resd 1 
    msg2: resd 1  



C++ version

 #include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <cmath>
#include <string>
#include <string.h>

using namespace std;

static int epsilon = 7;

static int randomNumber (int maxNum)
{
   return (rand() % maxNum ) + 1;
}

static void pick_actor()
{
   string actors[6] = {"Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"};
   int num = randomNumber(6);
   cout << actors[num-1] << " ";
}

static void pick_action()
{
   string actions[4] = { "buy", "sell", "deliver", "pick up"  };
   int num = randomNumber(4);
   cout << actions[num-1] << " ";
}

static void pick_acted()
{
   string acted[7] = { "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery" };
   int num = randomNumber(7);
   cout << acted[num-1] << " ";
}

static void order()
{
    cout << "ORDER : " ;
    pick_actor();
    pick_action();
    pick_acted();
    cout << endl;
}

static void order_list()
{
   while(1)
   { 
     int num = rand() % 100;
     if( num == epsilon )
     {
       cout << "EPSILON" << endl;
       return; 
     }
     order();
   } 
}

static void orders_of_the_day()
{
   cout << "Orders of the day : " << endl;
   cout << "====================" << endl;
   order_list();
}

int main(int argc, char *argv[] )
{
   orders_of_the_day();
}
    


Is This A Good Question/Topic? 0
  • +

Replies To: New to NASM and would like some help :)

#2 SegNotMyFault   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 19

Re: New to NASM and would like some help :)

Posted 05 November 2019 - 11:52 PM

Hello there fellow programers. I am really new to Nasm, I got into it because I know some MIPS and wanted to learn a new flavour of assembly. But to my surprise it has set up a goal to make my life very hard ahahah. Anywho I have written a program I translated from perl to C++ and then now I want to translate it to NASM assembly. I have all of it translated from the C++ code to the NASM code with all the lables and call I think it needs however when I run it it only prints the first two lines and then it thorws SegFault error type 8. I am really not able to track down where the error is coming from nor how to fix it. I have ran gbd on my C++ code but the NASM code I get from it is very far from the one I have written. I would really apreciate if anybody could help me see what am I doing wrong and how to fix it. Perhaps the code is not wrong but maybe the calling convention. Thank you all very much in advance!

Expected output

ORDERS OF THE DAY:
=========================
Order: Groceri1 Pick up Apples
Order: Market1 buy bread
Order: Warehouse1 sell asparagus
Order: Groceri2 deliver rice
EPSILON


NASM VERSION

 section .text
        global  _start



rand:                   ;result in eax
        rdtsc
        ret
print:
        mov     ebx, 1
        mov     eax, 4
        int     0x80
        ret

pick_actor: 
        ;call rand
        rdtsc
        mov ecx, 6
        div ecx    ; remainder is in edx
        
        mov eax, actors
        mov ecx, len1
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len11
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        
        
        call print
        ret
pick_action:
        rdtsc ;call rand
        mov ecx, 4
        div ecx    ; remainder is in edx
        
        mov eax, actions
        mov ecx, len2
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len22
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        call print
        ret
pick_acted:
        ;call rand
        rdtsc
        mov ecx, 7
        div ecx    ; remainder is in edx
        
        mov eax, acted
        mov ecx, len3
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len33
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        ret
order: 
        mov ecx, orderstr
        mov edx, len7
        call print
        
        call pick_actor
        call pick_action
        call pick_acted
        
        mov ecx, newline
        mov edx, 1
        call print
        ret



_start: 






          mov ecx, orders_of
          mov edx, len4
          call print
          
          mov ecx, separator
          mov edx, len5
          call print
          
          
loop1:                
         call rand  ; eax
         mov edx, 7
         mov ecx, 100
         div ecx    ; eax:edx
         sub ecx, edx
         jz loop2
         call order
         call loop1
loop2:  
         mov ecx, epsilon
         mov edx, len6
         call print
          
         call EXIT
         
EXIT:
         int 80h


    ;mov     ecx, msg
    ;mov     edx, len


        section   .data

actors:     db "Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"
len1:       db  0,  7,  14,  24,  34, 41
len11:      db  7,  7,  10,  10,   7,  7

actions:    db "buy", "sell", "deliver", "pick up"
len2:       db 0,3,7,14,21
len22:      db 3,4,7,7

acted:      db "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery"   
len3:       db 0,4,13,20,26,33,38,44
len33:       db 4,9,7,6,7,5,6

orders_of:  db "Orders of the day:",0xA
len4 equ $ - orders_of

separator:  db "====================",0xA
len5 equ $ - separator

epsilon:  db "EPSILON",0xA
len6 equ $ - epsilon

orderstr: db "ORDER : "
len7 equ $ - orderstr

newline:         db 0xA

SECTION .bss
    msg1: resd 1 
    msg2: resd 1  



C++ version

 #include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <cmath>
#include <string>
#include <string.h>

using namespace std;

static int epsilon = 7;

static int randomNumber (int maxNum)
{
   return (rand() % maxNum ) + 1;
}

static void pick_actor()
{
   string actors[6] = {"Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"};
   int num = randomNumber(6);
   cout << actors[num-1] << " ";
}

static void pick_action()
{
   string actions[4] = { "buy", "sell", "deliver", "pick up"  };
   int num = randomNumber(4);
   cout << actions[num-1] << " ";
}

static void pick_acted()
{
   string acted[7] = { "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery" };
   int num = randomNumber(7);
   cout << acted[num-1] << " ";
}

static void order()
{
    cout << "ORDER : " ;
    pick_actor();
    pick_action();
    pick_acted();
    cout << endl;
}

static void order_list()
{
   while(1)
   { 
     int num = rand() % 100;
     if( num == epsilon )
     {
       cout << "EPSILON" << endl;
       return; 
     }
     order();
   } 
}

static void orders_of_the_day()
{
   cout << "Orders of the day : " << endl;
   cout << "====================" << endl;
   order_list();
}

int main(int argc, char *argv[] )
{
   orders_of_the_day();
}
    


I'm so sorry guys I do not know why this keeps posting under the wrong thread I really do apologize I do not mean to clog it up
Was This Post Helpful? 0
  • +
  • -

#3 SegNotMyFault   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 19

Re: New to NASM and would like some help :)

Posted 05 November 2019 - 11:53 PM

Hello there fellow programers. I am really new to Nasm, I got into it because I know some MIPS and wanted to learn a new flavour of assembly. But to my surprise it has set up a goal to make my life very hard ahahah. Anywho I have written a program I translated from perl to C++ and then now I want to translate it to NASM assembly. I have all of it translated from the C++ code to the NASM code with all the lables and call I think it needs however when I run it it only prints the first two lines and then it thorws SegFault error type 8. I am really not able to track down where the error is coming from nor how to fix it. I have ran gbd on my C++ code but the NASM code I get from it is very far from the one I have written. I would really apreciate if anybody could help me see what am I doing wrong and how to fix it. Perhaps the code is not wrong but maybe the calling convention. Thank you all very much in advance!

Expected output

ORDERS OF THE DAY:
=========================
Order: Groceri1 Pick up Apples
Order: Market1 buy bread
Order: Warehouse1 sell asparagus
Order: Groceri2 deliver rice
EPSILON


NASM VERSION

 section .text
        global  _start



rand:                   ;result in eax
        rdtsc
        ret
print:
        mov     ebx, 1
        mov     eax, 4
        int     0x80
        ret

pick_actor: 
        ;call rand
        rdtsc
        mov ecx, 6
        div ecx    ; remainder is in edx
        
        mov eax, actors
        mov ecx, len1
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len11
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        
        
        call print
        ret
pick_action:
        rdtsc ;call rand
        mov ecx, 4
        div ecx    ; remainder is in edx
        
        mov eax, actions
        mov ecx, len2
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len22
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        call print
        ret
pick_acted:
        ;call rand
        rdtsc
        mov ecx, 7
        div ecx    ; remainder is in edx
        
        mov eax, acted
        mov ecx, len3
        add ecx, edx
        add eax, [ecx]
        mov ecx, eax
        
        mov ebx, len33
        add ebx, edx
        add ecx, [ebx]
        mov edx, ecx
        call print
        
        ret
order: 
        mov ecx, orderstr
        mov edx, len7
        call print
        
        call pick_actor
        call pick_action
        call pick_acted
        
        mov ecx, newline
        mov edx, 1
        call print
        ret



_start: 






          mov ecx, orders_of
          mov edx, len4
          call print
          
          mov ecx, separator
          mov edx, len5
          call print
          
          
loop1:                
         call rand  ; eax
         mov edx, 7
         mov ecx, 100
         div ecx    ; eax:edx
         sub ecx, edx
         jz loop2
         call order
         call loop1
loop2:  
         mov ecx, epsilon
         mov edx, len6
         call print
          
         call EXIT
         
EXIT:
         int 80h


    ;mov     ecx, msg
    ;mov     edx, len


        section   .data

actors:     db "Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"
len1:       db  0,  7,  14,  24,  34, 41
len11:      db  7,  7,  10,  10,   7,  7

actions:    db "buy", "sell", "deliver", "pick up"
len2:       db 0,3,7,14,21
len22:      db 3,4,7,7

acted:      db "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery"   
len3:       db 0,4,13,20,26,33,38,44
len33:       db 4,9,7,6,7,5,6

orders_of:  db "Orders of the day:",0xA
len4 equ $ - orders_of

separator:  db "====================",0xA
len5 equ $ - separator

epsilon:  db "EPSILON",0xA
len6 equ $ - epsilon

orderstr: db "ORDER : "
len7 equ $ - orderstr

newline:         db 0xA

SECTION .bss
    msg1: resd 1 
    msg2: resd 1  



C++ version

 #include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <cmath>
#include <string>
#include <string.h>

using namespace std;

static int epsilon = 7;

static int randomNumber (int maxNum)
{
   return (rand() % maxNum ) + 1;
}

static void pick_actor()
{
   string actors[6] = {"Grocer1", "Grocer2", "Warehouse1", "Warehouse2", "Market1", "Market2"};
   int num = randomNumber(6);
   cout << actors[num-1] << " ";
}

static void pick_action()
{
   string actions[4] = { "buy", "sell", "deliver", "pick up"  };
   int num = randomNumber(4);
   cout << actions[num-1] << " ";
}

static void pick_acted()
{
   string acted[7] = { "rice", "asparagus", "lettuce", "apples", "oranges", "bread", "celery" };
   int num = randomNumber(7);
   cout << acted[num-1] << " ";
}

static void order()
{
    cout << "ORDER : " ;
    pick_actor();
    pick_action();
    pick_acted();
    cout << endl;
}

static void order_list()
{
   while(1)
   { 
     int num = rand() % 100;
     if( num == epsilon )
     {
       cout << "EPSILON" << endl;
       return; 
     }
     order();
   } 
}

static void orders_of_the_day()
{
   cout << "Orders of the day : " << endl;
   cout << "====================" << endl;
   order_list();
}

int main(int argc, char *argv[] )
{
   orders_of_the_day();
}
    

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15497
  • View blog
  • Posts: 62,056
  • Joined: 12-June 08

Re: New to NASM and would like some help :)

Posted 06 November 2019 - 12:15 AM

Do not spam your topic. Merging duplicate threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1