14 Replies - 657 Views - Last Post: 15 October 2019 - 08:59 AM

#1 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

multiplication in assembly

Posted 11 October 2019 - 04:12 AM

Hi guys, what's up?
I'm trying to write complete code in assembly language to do C=A*B, C=|A-B|by just using those four instructions:
INC-(inc is increment the current value by 1)
DEC -(decrement the current value by 1)
JNZ-(jumping to specific label/point while THE VALUE of the previous row of instruction JNZ isn't equal to zero)
INC DEC JNZ HALT
for doing C=A*B, I coded this in assembly but in my assembly complier it doesn't work 100% and gives my wrong outputs of multiplication:
INC D //will be explained at
INC A
DEC A
JNZ LABEL1 //if A=0 -> A+1-1=0 then we will exit the program and c=0
if not it will jump to label1 //
HALT

LABEL1
INC B
DEC B
JNZ LABEL2 //same concept as above for register B
HALT

DEC B

LABEL2
INC B// In this loop register B gain again the value it lost at Label3 loop
DEC D//*the first time the program runs the value of D decreases to minus 1 that
//why we need to give it initial value of 1 by increasing its value by 1
JNZ LABEL2

LABEL3
INC C
INC D//each time this inner loop works ,the value of B goes to zero for that problem
DEC B//-the register D is gaining the value of B
JNZ LABEL3//Inner loop-runs on B

DEC A
JNZ LABEL2//outer loop-runs on A
HALT


any help how could I code C=A*B in a proper way and in a good approach of programming? in my code I included the case if A=0 or B=0 then we must exit the program because the question within its limitations of using instructions A,B can't be zero ..

thanks a lot!

Is This A Good Question/Topic? 0
  • +

Replies To: multiplication in assembly

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: multiplication in assembly

Posted 11 October 2019 - 11:30 AM

Try writing it with
- better loops names than LABEL[123]
- better variable names than [ABC]

Does your assembler permit you to have things like this to make better names for registers?
def MULTIPLIER A
def MULTIPLICAND B
def RESULT C



Start with a simple loop that allows you to achieve C = A * 1
Was This Post Helpful? 0
  • +
  • -

#3 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 11 October 2019 - 11:53 AM

No , but what's wrong with my code that I wrote? logically isn't it right?


thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#4 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 11 October 2019 - 04:13 PM

my problem is , I don't know how to do multiplication by + , - operation ! I mean I have INC and Dec and JNZ LABEL so how do I do multiplication ? sounds really tricky and I'm not getting the point of it.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: multiplication in assembly

Posted 11 October 2019 - 10:27 PM

> but what's wrong with my code that I wrote? logically isn't it right?
I've no idea.
It's an unreadable mess, so I didn't bother trying to grok the code.

I was rather hoping you'd improve the readability of your code, and in doing so, perhaps discover the actual problem you're facing.
Was This Post Helpful? 0
  • +
  • -

#6 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 12 October 2019 - 01:12 AM

lets assume I'm wrong, now my question to you I'm finding it hard to do multiplication by + and - and JNZ , may you gimme a hint? or a short code that I could start from it? much appreciated
Was This Post Helpful? 0
  • +
  • -

#7 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 12 October 2019 - 01:41 AM

for instance how can I do a simple code of assembly(included the limitations on specific given instructions in the question) to achieve C=A*1 ?
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: multiplication in assembly

Posted 12 October 2019 - 04:51 AM

This for example does a destructive C = A assignment.

clearC:
    DEC C
    JNZ clearC

addAtoC:
    INC C
    DEC A
    JNZ addAtoC



How many registers do you have (I see you mention D above).
Was This Post Helpful? 0
  • +
  • -

#9 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 12 October 2019 - 05:13 AM

it's under my control, could use whatever I want ..
Was This Post Helpful? 0
  • +
  • -

#10 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: multiplication in assembly

Posted 12 October 2019 - 09:51 PM

Well it's a process of stepwise refinement.

You start with the highest level operation
MOV C,0     // the result
MUL C,A,B   // C = A*B



Then you replace MUL with repeated addition
MOV C,0     // the result
  addloop   // MUL C,A,B
  ADD C,B
  DEC A
  JNZ addloop



Then you replace ADD with INC
MOV C,0
  addloop
    MOV D,B     
    incloop   // ADD C,B
    INC C
    DEC D
    JNZ incloop
  DEC A
  JNZ addloop



Then YOU replace MOV with some combination of INC/DEC/JNZ.
I've already showed you how to clear a register.

Seriously, start adopting a process where you step-wise refine your answers.
Attempting to go straight for the answer every time will doom you to failure.


For extra fun, reduce your four instructions to one.
https://en.wikipedia...on_set_computer
Was This Post Helpful? 0
  • +
  • -

#11 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 13 October 2019 - 01:50 AM

clearC:
 DEC C
 JNZ clearC
addAtoC:
   INC C
   DEC A
   JNZ addAtoC
addloop
clearC:
 DEC C
 JNZ clearC
addAtoC:
   INC C
   DEC A
   JNZ addAtoC
incloop   // ADD C,B
INC C
DEC D
JNZ incloop
DEC A
JNZ addloop




Hi salem, I appreciate your effort to help me , thanks alot!

I've arrived to this upper code that's doing multiplication C=A*B is it right? I'm not that much skilled so I'm trying my best and hope it's correct ..

In addition, but if A=0 or B=0 is zero then the code will not be executed .. because the row before JNZ will be always zero so will not do a loop .. in this case I must Jump to "HALT" for exiting the code .. so what you suggest me to DO in case A=0 or B=0? thanks alot ..
Was This Post Helpful? 0
  • +
  • -

#12 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 13 October 2019 - 02:13 AM

this is the fixed code .. hope it's right now
clearC:
 DEC C
 JNZ clearC
addAtoC:
   INC C
   DEC A
   JNZ addAtoC
addloop
clearC:
 DEC C
 JNZ clearC
addAtoC:
   INC C
   DEC D
   JNZ addAtoC
incloop   // ADD C,B
INC C
DEC D
JNZ incloop
DEC A
JNZ addloop




Was This Post Helpful? 0
  • +
  • -

#13 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 13 October 2019 - 09:01 AM

Am I right? thanks salem, you helped me much !
Was This Post Helpful? 0
  • +
  • -

#14 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: multiplication in assembly

Posted 15 October 2019 - 08:53 AM

View PostSalem_c, on 11 October 2019 - 11:30 AM, said:

Try writing it with
- better loops names than LABEL[123]
- better variable names than [ABC]

Does your assembler permit you to have things like this to make better names for registers?
def MULTIPLIER A
def MULTIPLICAND B
def RESULT C



Start with a simple loop that allows you to achieve C = A * 1


Hi salem_c may ou please tell me if what I've coded is right or not?
I can't know because it's not a uniform language that compilers identified with, so it would be really appreciated if you could help me if what I coded in my last comment is correct or not ..
Was This Post Helpful? -1
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



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

Re: multiplication in assembly

Posted 15 October 2019 - 08:59 AM

Please remember this is a volunteer community. Do not excessively bump, or beg, for attention.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1