Author Topic: Multiplication shift & add  (Read 6089 times)

Offline sahil

  • Jr. Member
  • *
  • Posts: 4
Multiplication shift & add
« on: April 19, 2014, 08:56:07 AM »
got stuck in shift and add
section .data
   msg db 10,'Enter a 4 digit number',10
   msgl: equ $-msg
   msg1 db 'The mul is',0ah
   msg1l:equ $-msg1

section .bss
   num1 resb 10
   num2 resb 10
   temp resd 10
   dispnum resb 8
   ans resq 1
%macro cmn 4
   mov eax,%1
   mov ebx,%2
   mov ecx,%3
   mov edx,%4
   int 80h
%endm
section .text
   global  _start
_start:
   cmn 4,1,msg,msgl
   cmn 3,0,temp,5

   call process
   mov [num1],ebx

   cmn 4,1,msg,msgl
   cmn 3,0,temp,5

   call process
   mov [num2],ebx
   
   call addition
   ;mov eax,[edx]
   ;call disp

ext:
   mov eax,1
   mov ebx,0
   int 80h
   
process:
   
   xor ebx,ebx
   mov ecx,4
   mov esi,temp

up1:
   xor eax,eax
   rol ebx,4
   mov al,[esi]
   cmp al,39h
   jbe xyz
   cmp al,59h
   jbe xyz1
   sub al,20h
xyz1:
   sub al,07h
xyz:
   sub al,30h
   add ebx,eax
   inc esi
   loop up1
   ret

addition:
   mov ebx,0h
   mov eax,[num1]
   mov ebx,[num2]
   
   mov edx,0000h
   
   mov ecx,16
dwi:
   shl edx,1
   shl eax,1

   jnc ab
   add edx,ebx
ab:
   dec ecx
   jnz dwi
   
   mov [ans],edx

   
   call disp
ret

disp:   mov eax,[ans]
    mov esi,dispnum+7
    mov ecx,8
cntdz:
    mov edx,0
    mov ebx,10h
    div bx
    cmp dl,09h
    jbe pqrz
    add dl,07h
pqrz:
    add dl,30h
    mov [esi],dl
    dec esi
    loop cntdz
   
    cmn 4,1,dispnum,8
   ret
« Last Edit: April 19, 2014, 09:20:51 AM by sahil »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Multiplication shift & add
« Reply #1 on: April 19, 2014, 09:00:32 AM »
got stuck in shift and add

Cool!
Encryptor256's Investigation \ Research Department.

Offline sahil

  • Jr. Member
  • *
  • Posts: 4
Re: Multiplication shift & add
« Reply #2 on: April 19, 2014, 09:12:06 AM »
buddy help me out ! cool isn't enough .....

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2373
  • Country: us
Re: Multiplication shift & add
« Reply #3 on: April 19, 2014, 09:50:24 AM »
You start out prompting for a four digit number, then read 3 bytes... into a 2 byte buffer.You load the contents of two 1 byte variables num1 and num2 into eax and ebx. You need to go over your program from top to bottom. Think about how big your variables need to be. It won't do any harm if they're bigger than they need to be, but it is a grave error to read or write more than the buffer will hold. Oh wait a minute... you've edited this since I looked at it. Never mind...

When you want help with a problem, tell us what you expected it to do, and what happened instead. "stuck" isn't much information. A guy named Brendan over on Stack Overflow claims there are only two possible errors in assembly language. Either the comment doesn't describe a correct algorithm, or the code doesn't do what the comment says. Keep that in mind when you write some more comments in your code.

Best,
Frank


Offline sahil

  • Jr. Member
  • *
  • Posts: 4
Re: Multiplication shift & add
« Reply #4 on: April 22, 2014, 01:19:05 PM »
my problem is whenever I enter a no & store it in temp and call for process[to convert entered no ascii format to its hex equivalent ] I failed to get correct equivalent number. Is it because of wrong reserved memory or from wrong rotation process? When I entered a no & print that using disp procedure: My entered no-7899
                no echoed after process- 0000F132 .here i need 7899 ! How come I am getting this?   :o

Offline z80a

  • Jr. Member
  • *
  • Posts: 20
Re: Multiplication shift & add
« Reply #5 on: April 22, 2014, 07:44:59 PM »
Look at my post here for inputting a ASCII decimal number and storing as binary:

http://forum.nasm.us/index.php?topic=1866.0

Result is in register RAX, add breakpoint in debugger just before exit register load.

Offline mega

  • Jr. Member
  • *
  • Posts: 4
Re: Multiplication shift & add
« Reply #6 on: May 20, 2014, 06:51:47 AM »
Thought I'd add my example to this thread .. Linux 32bit version

Code: [Select]
; Multiply 2 integers using shift and add
BITS 32
%include "linux_syscalls_32.inc"

section .data
buffer: times 128 db 0

section .bss

section .text
global _start
extern lenstr
extern adton
extern ntoad
extern prtstr

_start:
; get arg count
pop ecx
cmp ecx, 3
je main           ; continue if we have the correct number of args
mov ebx, 2        ; .. otherwise badboy exit
exit:
mov eax, 1
int 0x80

main:
pop edx           ; throw away prog name
call lenstr       ; get string length - 1st arg is already at top of stack
pop edx
push eax
push edx
call adton        ; convert string arg to number
add esp, 8
mov esi, eax
pop edx           ; grab second arg
push esi          ; save first arg
push edx
call lenstr       ; get string length
add esp, 4
push eax
push edx
call adton        ; convert string arg to number
add esp, 8
pop esi           ; restore first arg
mov ebx, eax
;
xor ecx, ecx      ; init exponent indicator
xor eax, eax      ; init accumulator
mply:
cmp ebx, 0        ; are we finished?
jz finished       ; leave
sar ebx,1         ; move least bit of multiplier into carry flag
jnc noaction      ; if CF == 0 then no action required
mov edx, esi      ; copy first num into eax
shl edx, cl       ; multiply by 2 cl times
add eax, edx      ; add result to accumulator ** overflow check ?
noaction:
inc ecx           ; increment exponent indicator
jmp mply          ; loop
finished:

push eax
push buffer
call ntoad        ; number to ascii decimal: ntoad( char *string, int number )
add esp, 8

push buffer
call lenstr       ; get string length
add esp, 4

push eax
push buffer
call prtstr       ; print the result
add esp, 8

mov ebx, 0        ; goodboy exit
jmp exit