5 Replies - 17566 Views - Last Post: 26 October 2010 - 03:21 AM

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Week 32- Assembly

Post icon  Posted 24 September 2010 - 07:32 PM

This weeks challenge is Assembly language!

Assembly language is a low-level programming language designed for hardware-level interactions, such as processor and microcontroller programming. Assembly statements generally represent one-to-one relations to corresponding machine code instructions. These assembly statements are translated to machine code by an Assembler.

Getting Started:
Below are some of the more popular Assembler options available. Note that GoASM is for Windows only.
  • NASM - the Netwide Assembler
  • FASM - the Flat Assembler
  • GoASM - the Go Assembler



References and Help:
-The DIC Assembly Tutorials in the Other Languages Forum
-The DIC Assembly Help Forum
-The Intel Software Developer Manuals

Is This A Good Question/Topic? 0
  • +

Replies To: Week 32- Assembly

#2 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Week 32- Assembly

Posted 25 September 2010 - 09:05 AM

I find the easiest way if you want to get straight into using assembly is to use it inline with the gcc compiler

http://www.ibiblio.o...mbly-HOWTO.html

An example usage

#include <stdio.h>

int main() {
    /* Add 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                "movl $20, %ebx;"
                "addl %ebx, %eax;"
    );

    /* Subtract 20 from 10 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "subl %ebx, %eax;"
    );

    /* Multiply 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "imull %ebx, %eax;"
    );

    return 0 ;
}

Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Week 32- Assembly

Posted 26 September 2010 - 07:23 AM

.model small
.stack
.data
	pares1 db "Even: ", '$'
	impares1 db "Odd: ", '$'
	ingresar1 db "Type in 20 numbers" '$'
	titulo db "Program to separate odd and even numbers", '$'
	enterar db 10,13,'$'
	contimpares dw 0
	contpares dw 0
.code
imprimir proc
	mov ah, 09h
	int 21h
	ret
imprimir endp
ingresar proc
	mov ah, 01h
	int 21h
	ret
ingresar endp
imprimir2 proc
	mov ah, 2h
	int 21h
	ret
imprimir2 endp
salir proc
	pop bx
	mov cx, contpares
	lea dx, enterar
	call imprimir
	lea dx, pares1
	call imprimir
	looppares:
		pop dx
		call imprimir2
	loop looppares
	lea dx, enterar
	call imprimir
	lea dx, impares1
	call imprimir
	mov cx, contimpares
	loopimpares:
		pop dx
		call imprimir2
	loop loopimpares
	mov ax, 4c00h
	int 21h
	ret
salir endp
ingresopila proc
	mov cx, 20
	pop bx
	loops:
		call ingresar
		push ax
	loop loops
	push bx
	ret
ingresopila endp
impares proc
	pop bx
	cont:
		mov ax, [bp + si]
		and ax, 01
		cmp ax, 1
			je impar
		add si, 2
	loop cont
	push bx
	ret
	impar:
		push [bp + si]
		add si, 2
		inc contimpares
	loop cont
	push bx
	ret
impares endp
pares proc
	pop bx
	cont2:
		mov ax, [bp + si]
		and ax, 01
		cmp ax, 0
			je par
		add si, 2
	loop cont2
	push bx
	ret
	par:
		push [bp + si]
		add si, 2
		inc contpares
	loop cont2
	push bx
	ret
pares endp
main:
	mov dx, @data
	mov ds, dx
	lea dx, titulo
	call imprimir
	lea dx, enterar
	call imprimir
	lea dx, ingresar1
	call imprimir
	lea dx, enterar
	call imprimir
	call ingresopila
	mov si, 0
	mov cx, 20
	mov bp, sp
	call impares
	mov si, 0
	mov cx, 20
	call pares
	lea dx, enterar
	call imprimir
	call salir
end main


Its for tasm, and its assembly 8086 i hope that can be used too xD

This post has been edited by poncho4all: 26 September 2010 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Re: Week 32- Assembly

Posted 27 September 2010 - 10:05 AM

Here is my code submitted for "Hello World" in every language.

I had fixed the segfault at one point, but I forget where the issue is :

; nasm -f elf -o input.o input.asm
; ld -s -o input input.o
section .data
var:  DB 0              ; declare var with value 0
don:  DB 10             ; declare our done value at 10
msg:  DB "Please enter your name: "
len:  equ $-msg ; the length of msg
dic:  DB "Welcome to Dream In Code, "
dic_ln: equ $-dic
buffer: DB 48 ; input buffer = 48 bytes
bf_sz: equ $-buffer

section .text
global _start

_start:
  mov eax, 4; pass sys_write
  mov ebx, 1; stdout
  mov ecx, msg; Load register ecx with prompt msg
  mov edx, len; Load register edx with length of msg
  int 0x80      ; return to the kernel
  jmp stdin  ; Go to input function

stdout:
   mov eax, 4; pass sys_write
   mov ebx, 1; stdout
   mov ecx, dic
   mov edx, dic_ln
   int 0x80; return to the kernel

   mov eax, 4; pass sys_write
   mov ebx, 1; stdout
   mov ecx, buffer; display the buffer contents
   mov edx, bf_sz; size of buffer
   int 0x80      ; return to the kernel
   jmp end       ; We are all finished

stdin:
; Get text from keyboard buffer
  mov eax, 3; pass sys_read
  xor ebx, 0; stdin
  mov ecx, buffer
  mov edx, bf_sz; count
  int 0x80      ; return

  jmp stdout; Go to display function

end:
  ret



** Update! **

The issue is simple this : The code needs the equivalent of return 0 from main.

in function end :

end:
  mov eax, 1 ; call sys exit
  mov ebx, 0 ; call return 0
  int 0x80   ; call the kernel
  ret



No more segfault ;)
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Week 32- Assembly

Posted 30 September 2010 - 07:32 AM

I would like to submit to the tools list Eric Isaacson's a86 assembler which can be downloaded for free from http://eji.com/a86/. While it is only a 16 bit assembler (won't work on 64 bit windows) I find it to be a much more friendly than MASM.
Was This Post Helpful? 1
  • +
  • -

#6 oldisgold  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-October 10

Re: Week 32- Assembly

Posted 26 October 2010 - 03:21 AM

i have joined d.i.c just a few minutes ago & seen these posts.
though the assy week is long gone, i venture to let u have one of my programs here.

this is something i had written some 10 years ago in masm v2.0 & recently amended to run in masm v6.11. the code switches the system to vga 16-color graphics mode & displays 16 vertical bands of the 16 colors of vga.

; program to test VGA 640x480 pixel, 16-colour graphics mode.
; draws 16 vertical bands of different colours on the screen.

; this program also demonstrates how to mix text & graphics.

.model  small

data    segment  'data'
   row   DW    0
   col   DW    0
   color DB    0
   ctr1  DB    0
   ctr2  DB    0
   ctr3  DW    0
   trow  db    0
   tcol  db    0
   msg   DB    "Press any key to continue..."
data    ends

assume   CS:code,   DS:data

code    segment   'code'
         MOV   AX,   data
         MOV   DS,   AX

main	proc
         MOV   AH,   0        ; set VGA graphics mode
         MOV   AL,   12h
         INT   10h

         MOV   ctr1, 16
   M1:   CALL  band           ; display a colour band
         INC   color
         DEC   ctr1
         JNZ   M1

         ; display prompt (text)
         MOV   TROW, 29				; position text cursor
         MOV   TCOL, 25				; suitably to display 
         MOV   CTR1, 28				; msg at bottom of screen.
         MOV   SI,   OFFSET   MSG		
   M2:   CALL  SETCUR				
         MOV   AH,   09
         MOV   AL,   [SI]
         MOV   BH,   0
         MOV   BL,   15
         MOV   CX,   1
         INT   10h
         INC   TCOL
         INC   SI
         DEC   CTR1
         JNZ   M2

         MOV   AH,   0        ; wait till keypress
         INT   16h

         MOV   AH,   0        ; set text mode
         MOV   AL,   2
         INT   10h

         MOV   AX,   4C00h    ; terminate program and
         INT   21h            ; return to DOS
main     endp


band     proc
         MOV   ctr2, 40
P2:      CALL  line           ; display one vertical line
         INC   col
         DEC   ctr2
         JNZ   P2
         RET
band     endp


line     proc
         MOV   row,  0
         MOV   ctr3, 480		; 1 vertical line=480 pixels
	
P3:      MOV   AH,   0Ch		; display a pixel
         MOV   AL,   color		; of desired colour
         MOV   CX,   col		; at desired position
         MOV   DX,   row		; on screen
         INT   10h

         INC   row
         DEC   ctr3
         JNZ   P3
         RET
line     endp


setcur   proc
         MOV   AH,   02h		; set cursor
         MOV   BH,   0			; in text page 0
         MOV   DH,   TROW		; at desired
         MOV   DL,   TCOL		; position 
         INT   10h			; on screen
         RET
setcur   endp

code    ends
end


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1