Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[NASM] how to change some codes and link?

assembly

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 30 September 2014 - 07:43 AM

Hi there. thansk for your help.
I find an Assembly program that I can run in my OS (Mac OS X)
So this is the code :

---------------------------------
 

; hello.asm - a "hello, world" program using NASM

section .text

global mystart ; make the main function externally visible

mystart:

; 1 print "hello, world"

; 1a prepare the arguments for the system call to write
push dword mylen ; message length
push dword mymsg ; message to write
push dword 1 ; file descriptor value

; 1b make the system call to write
mov eax, 0x4 ; system call number for write
sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack
int 0x80 ; make the actual system call

; 1c clean up the stack
add esp, 16 ; 3 args * 4 bytes/arg + 4 bytes extra space = 16 bytes

; 2 exit the program

; 2a prepare the argument for the sys call to exit
push dword 0 ; exit status returned to the operating system

; 2b make the call to sys call to exit
mov eax, 0x1 ; system call number for exit
sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack
int 0x80 ; make the system call

; 2c no need to clean up the stack because no code here would executed: already exited

section .data

mymsg db "hello, world", 0xa ; string with a carriage-return
mylen equ $-mymsg ; string length in bytes

--------------------------
nasm -f macho hello.asm
ld -o hello -e mystart hello.o
that's it.
--------------------------
Now I want to run this program :

.MODEL SMALL
.DATA
VAL1 DB ?
NL1 DB 0AH,0DH,'ENTER HOW MANY NO U WANT:','$'
NL2 DB 0AH,0DH,'ENTER NO:','$'
NL3 DB 0AH,0DH,'AVEARGE:','$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX

LEA DX,NL1
MOV AH,09H
INT 21H

MOV AH,01H
INT 21H
SUB AL,30H

MOV CL,AL
MOV BL,AL
MOV AL,00
MOV VAL1,AL

LBL1:
LEA DX,NL2
MOV AH,09H
INT 21H

MOV AH,01H
INT 21H
SUB AL,30H

ADD AL,VAL1
MOV VAL1,AL
LOOP LBL1

LBL2:
LEA DX,NL3
MOV AH,09H
INT 21H

MOV AX,00
MOV AL,VAL1
DIV BL
ADD AX,3030H
MOV DX,AX
MOV AH,02H
INT 21H

MOV AH,4CH
INT 21H

MAIN ENDP
END MAIN


OUTPUT
************

Z:\SYSTEM~1\AS1>ex11

ENTER HOW MANY NO U WANT:3
ENTER NO:3
ENTER NO:3
ENTER NO:3
AVEARGE:3

this application takes three numbers from user and calculates the average.
how can I add a linker and run it.
thanks ;)


Edited by dargueta, 30 September 2014 - 01:34 PM.
Added code tags.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 30 September 2014 - 01:39 PM

The other program is in MASM, a different dialect of Intel assembly language specific to Microsoft and is only available on Windows. Furthermore, it's 16-bit (i.e. hella old) and won't work with Windows anymore because it uses BIOS interrupts.

 

really really strongly suggest you learn about Intel assembly language (preferably the NASM dialect) before you try running anything.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 30 September 2014 - 06:24 PM

OK. But can anyone correct this code for me?
I want to show it to someone for comparing Assembly and C syntax.
please help me.

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 30 September 2014 - 09:25 PM

This is a bad example for that. However, I'll humor you and translate to NASM. Again, I must stress that this code will not work on any modern OS.

.bits 16

section .bss
val1:   resb 1

section .data
nl1: db 0x0d, 0x0a, "ENTER HOW MANY NO U WANT:$"
nl2: db 0x0d, 0x0a, "ENTER NO:$"
nl3: db 0x0d, 0x0a, "AVERAGE:$"

section .text
main:
    lea     dx, nl1
    mov     ah, 0x09
    int     0x21
    
    mov     ah, 0x01
    int     0x21
    sub     al, 0x30
    
    mov     cl, al
    mov     bl, al
    mov     al, 0
    mov     [val1], al
    
    .lbl1:
        lea     dx, nl2
        mov     ah, 0x09
        int     0x21
        
        mov     ah, 0x01
        int     0x21
        sub     al, 0x30
        
        add     al, [val1]
        mov     [val1], al
        loop    .lbl1
        
    .lbl2:
    lea     dx, nl3
    mov     ah, 0x09
    int     0x21
    
    mov     ax, 0x00
    mov     al, [val1]
    div     bl
    add     ax, 0x3030
    mov     dx, ax
    mov     ah, 0x02
    int     0x21
        
    mov     ah, 0x4c
    int     0x21

Edited by dargueta, 30 September 2014 - 09:26 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 01 October 2014 - 05:08 AM

So how can I convert it for running on Mac OS?
I have to teach someone. If the program doesn't work they don't trust me:(

Edited by SnakeS, 01 October 2014 - 05:10 AM.


#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 01 October 2014 - 09:00 AM

You can't teach someone if you don't know what you're doing. To get it to run, you must:

 

1) Convert it to either 32- or 64-bit.

2) Rewrite it to use OSX system calls instead of BIOS interrupts, which aren't available in protected mode.


Edited by dargueta, 01 October 2014 - 09:05 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 07 October 2014 - 04:26 AM

hi again

this is my code :

section .bss            ;Uninitialized data
    num resb 5
section .text


global mystart                ; make the main function externally visible


mystart:
       ;User prompt
       mov eax, 4
       mov ebx, 1
       mov ecx, userMsg
       mov edx, lenUserMsg
       int 0x80


       ;Read and store the user input
       mov eax, 3
       mov ebx, 2
       mov ecx, num  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
       int 0x80
       ;Output the message 'The entered number is: '
       mov eax, 4
       mov ebx, 1
       mov ecx, dispMsg
       mov edx, lenDispMsg
       int 0x80  


       ;Output the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, num
       mov edx, 5
       int 0x80
       
       ;////////////////  
mov eax, 0x1              ; system call number for exit
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the system call


section  .data ;Data segment
    userMsg db 'Please enter a number: ' ;Ask the user to enter a number
    lenUserMsg equ $-userMsg             ;The length of the message
    dispMsg db 'You have entered: '
    lenDispMsg equ $-dispMsg
 
                
It wil end before taking the number from user.
 
 
 
 

SnakeS:Desktop SnakeS$ nasm -f macho aa.asm

SnakeS:Desktop SnakeS$ ld -o aa -e mystart aa.o

ld: warning: -macosx_version_min not specified, assuming 10.8

SnakeS:Desktop SnakeS$ ./aa

SnakeS:Desktop SnakeS$ ./aa

 
 
 
 the warning you see is not important
thanks again ;)

Edited by dargueta, 07 October 2014 - 07:00 AM.
Added code tags


#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 07 October 2014 - 07:05 AM

When reading the user input, try changing mov ebx, 2 to mov ebx, 0.


Edited by dargueta, 07 October 2014 - 07:10 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#9 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 08 October 2014 - 07:55 AM

fixed ,thanks a lot

another question :

I made this program for average :

section .bss            ;Uninitialized data
    average resb 5
 num1 resb 5
num2 resb 5
num3 resb 5
section .text

global _start                ; make the main function externally visible

_start:
       ;User prompt
       mov eax, 4
       mov ebx, 1
       mov ecx, messaging
       mov edx, lenmessaging
       int 0x80

       ;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num1  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
       mov ebx, 1
       mov ecx, messaging
       mov edx, lenmessaging
       int 0x80
;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num2  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
       mov ebx, 1
       mov ecx, userMsg
       mov edx, lenUserMsg
       int 0x80
;Read and store the user input
       mov eax, 3
       mov ebx, 0
       mov ecx, num3  
       mov edx, 5       ;5 bytes (numeric, 1 for sign) of that information
       int 0x80
       ;Output the message
       mov eax, 4
       mov ebx, 1
       mov ecx, dispMsg
       mov edx, lenDispMsg
       int 0x80  
 
              ; moving the first number to eax register and second number to ebx
    ; and subtracting ascii '0' to convert it into a decimal number
    mov eax, [num1]
    sub eax, '0'
    mov ebx, [num2]
    sub ebx, '0'

    ; add eax and ebx
    add eax, ebx
    mov ebx, [num3]
    sub ebx, '0'
        add eax, ebx

    
  
	mov 	bl, '3'
	sub     bl, '0'
	div 	bl
	add eax, '0'
       mov [average], eax

       ;Output the number entered
       mov eax, 4
       mov ebx, 1
       mov ecx, average
       mov edx, 5
       int 0x80

       ;////////////////  
mov eax, 0x1              ; system call number for exit
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the system call

section  .data ;Data segment
    userMsg db 'Please enter a number: ' ;Ask the user to enter a nu
    lenUserMsg equ $-userMsg             ;The length of the message
    dispMsg db 'The average is : '
    lenDispMsg equ $-dispMsg  
messaging db 'Enter another number : '
    lenmessaging equ $-messaging
              

but it has a problem :

 

Screen Shot 2014-10-08 at 7.23.42 PM.png



#10 gonerogue

gonerogue

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 197 posts

Posted 08 October 2014 - 09:49 AM

When executing this instruction:

div     bl

the content of eax is not what you expect (hence the floating point exception).


Edited by gonerogue, 08 October 2014 - 09:53 AM.


#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 October 2014 - 10:13 AM

Actually, no, that just means that bl is 0. Dividing by 0 is a problem. This will happen whenever the place you're looking at contains '0'. For example, "10" will explode when you try to divide the last digit.


Edited by dargueta, 08 October 2014 - 10:14 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#12 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 08 October 2014 - 10:17 AM

so can you correct the code?
I need it for 5 hours later.
for showing to my teacher.
thanks ;)
so can you correct the code?
I need it for 5 hours later.
for showing to my teacher. m
thanks ;)




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download