Easy68K IF-ELSE branching - easy68k

writing my first assembly language program for class using Easy68K.
I'm using an if-else branching to replicate the code:
IF (P > 12)
P = P * 8 + 3
ELSE
P = P - Q
PRINT P
But I think I have my branches wrong because without the first halt in my code the program runs through the IF branch anyway even after the CMP finds a case that P < 12. Am I missing something here or would this be a generally accepted way of doing this?
Here is my assembly code:
START: ORG $1000 ; Program starts at loc $1000
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BGT IF ;
SUB D2, D1 ; P = P - Q
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
IF ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
* Data and Variables
ORG $2000 ; Data starts at loc $2000
P DC.W 5 ;
Q DC.W 7 ;
END START ; last line of source

To do if..else, you need two jumps; one at the start, and one at the end of the first block.
While it doesn't affect correctness, it is also conventional to retain source order, which means negating the condition.
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BLE ELSE ; P is <= 12
IF
ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
BRA ENDIF
ELSE
SUB D2, D1 ; P = P - Q
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator

EASy68K supports structured assembly.
OPT SEX
IF.L P <GT> #12 THEN
ELSE
ENDI
Add the option SEX to expand the structured code during assembly if you wish to view the compare and branch instructions used to implement the structured code.

Related

GO TO statements- Fortran to Matlab

I have been trying really hard to convert this grid search code from Fortran to Matlab, however I am not able to properly incorporate the GO TO statements (I am trying to use while loops but I think I need something else for them end the search). Any help would be greatly appreciated.
vmax = -1.0E+15
amax_G = -1
askipR = REAL(ac_max - ac_min)/REAL(intA)
askip = CEILING(askipR)
DO acc0 = 1,intA+1
acc = ac_min + askipR * (acc0-1)
cons = ( x1 - grida2(acc) ) / onetauc
IF (cons<0.0) GOTO 102
vtemp = utilR(cons) + one_sv * utilB(acc)
IF (vtemp>vmax) THEN
vmax = vtemp
amax_G = acc
ELSE
GOTO 102
ENDIF
ENDDO ! acc0
102 continue
IF (askip<2) GO TO 109
askip = askip/2
IF (amax_G>ac_min) THEN
acc = amax_G - askip
cons = ( x1 - grida2(acc) ) / onetauc
IF (cons<0.0) PRINT *,'WARNING: NEGATIVE CONS # ASEARCH_Rx'
vtemp = utilR(cons) + one_sv * utilB(acc)
IF (vtemp>vmax) THEN
vmax = vtemp
amax_G = acc
GOTO 102
ENDIF
ENDIF
IF (amax_G < ac_max) THEN
acc = amax_G + askip
cons = ( x1 - grida2(acc) ) / onetauc
IF (cons<0.0) GO TO 102
vtemp = utilR(cons) + one_sv * utilB(acc)
IF (vtemp>vmax) THEN
vmax = vtemp
amax_G = acc
ENDIF
ENDIF
GOTO 102
109 CONTINUE
vfunR(jc,ac,sc) = vmax ! jc=Nj
afunR_G(jc,ac,sc) = amax_G ! jc=Nj
You have two types of control-flow implemented with your GOTO statement:
Type 1: breaking out of a loop
The GOTO statement is used to break out of the DO-loop:
DO acc0 = 1,intA+1
! some code
IF (cons<0.0) GOTO 102
! some more code
END DO
102 continue
As you notice, if cons < 0.0 the GOTO statement states to move to label 102 which is located just outside of the DO-loop. In matlab is nothing more than a simple break in a for-loop:
for acc0=1:intA+1
% some matlab code
if (cons < 0.0)
break
end
% some more matlab code
end
Type 2: creating a loop
While the loop is not explicitly written, the following code creates something that can be translated as a while loop:
! This is the start of the loop
102 continue
! This is the condition to exit the loop <<==
IF (askip<2) GO TO 109
! stuff
! This is a condition to restart the loop <<==
IF (vtemp>vmax) THEN
vmax = vtemp
amax_G = acc
GOTO 102
ENDIF
! stuff
! This is another condition to restart the loop <<==
IF (cons<0.0) GO TO 102
! stuff
! This is the end of the loop, asked to restart it <<==
GOTO 102
! This is outside of the loop
109 CONTINUE
In the end, you translate this as:
while (askip >=2)
% stuff
if (vtemp > vmax)
vmax = vtemp
amax_G = acc
continue
end
% stuff
if (cons < 0.0)
continue
end
% stuff
end

Undefined operator '>' for input arguments of type 'msspoly' (optimization software)

I know there are many similar questions like the title ask here.
However, I still believe no of these discussions can solve my problem.
I am testing the following software "SPOTless" developed by MIT:
https://github.com/spot-toolbox/spotless
This is a software which transform the polynomial optimization problem to LP or SOCP. And then call the other software "mosek" to solve it.
This example is from the tutorial in:
https://github.com/spot-toolbox/spotless/tree/master/doc (please see the last example)
n = 2 ;
d = 4 ;
x = msspoly('x',n ) ;
basis = monomials(x , 0 : d ) ;
p = randn(length(basis))'*basis ;
g = 1 - x'*x ;
prog = spotsosprog ;
prog = prog.withIndeterminate(x) ;
[prog,r] = prog.newFree(1) ;
[prog,f] = prog.newFreePoly(x,monomials(x,0:d-2)) ;
prog = prog.withSOS(r-p-f*g) ;
sol = prog.minimize(r) ;
However, when run it, Matlab shows the following error:
The line 401 and the related part is the following:
function [pr,poly,coeff] = newFreePoly(pr,basis,n)
if nargin < 3, n = 1; end
if ~isempty(basis) && n > 0 % line 401
if ~pr.isPolyInIndet(basis)
error('Basis must be polynomial in the indeterminates.');
end
[pr,coeff] = pr.newFree(length(basis)*n);
poly = reshape(coeff,n,length(basis))*basis;
else
poly = [];
end
end
$n$ is an integer, so $n > 0$ is valid obviously.
I ask the related professor (not the author of this software); he told me he has not seen this problem before. The problem comes from one .m file in the software.
I am not sure if someone can help me test this software and this example and let me know the result or how to solve this error.
Note: if you really test this software (easy to install), please also install "mosek", which is a famous optimization software.
Note: if you do not want to install "mosek", please cross out the last two lines in the code.

Julia macro for transforming `f(dim1,dim2,..) = value` into `f(value,dim1,dim2,..)`

I am trying to write a Julia macro that transforms this:
[par1!( par2(d1,d2)+par3(d1,d2) ,d1,d2,dfix3) for d1 in DIM1, d2 in DIM2]
(not very inspiring) into something much more readable, like this:
#meq par1!(d1 in DIM1, d2 in DIM2, dfix3) = par2(d1,d2)+par3(d1,d2)
where par1!() is a function to set some multi-dimensional data and par2() is a getData()-type of function.
I am trying to implement it using a macro, but as I am on my first experience with julia marcro, I'm not sure how to "assemble" the final expression from the various pieces..
Here is what I done so far:
macro meq(eq)
# dump(eq)
lhs_par = eq.args[1].args[1]
rhs = eq.args[2]
lhs_dims = eq.args[1].args[2:end]
loop_counters = [d.args[2] for d in lhs_dims if typeof(d) == Expr]
loop_sets = [d.args[3] for d in lhs_dims if typeof(d) == Expr]
loop_wholeElements = [d for d in lhs_dims if typeof(d) == Expr]
lhs_dims_placeholders = []
for d in lhs_dims
if typeof(d) == Expr
push!(lhs_dims_placeholders,d.args[2])
else
push!(lhs_dims_placeholders,d)
end
end
outExp = quote
[$(lhs_par)($(rhs),$(lhs_dims_placeholders ...)) for $(loop_wholeElements ...) ]
end
#show(outExp)
return outExp
end
However the above macro doesn't compile and returns a syntax error (“invalid iteration specification”) due to the for $(loop_wholeElements) part… indeed I don’t know how to treat the expressions in lhs_dims_placeholders and loop_wholeElements in order to “assemble” the expanded expression…
EDIT:
The example posted, with d1, d2 and dfix3, is only a specific case, but the macro should be able to handle whichever dimensions are looped for..
I think the macro up there does that, but I don't know how to build the final expression.. :-(
Instead of manually doing those hard-coded args matching stuff, we could use MacroTools.jl as a handy tool for template matching:
julia> using MacroTools
julia> macro meq(ex)
#capture(ex, f_(d1_ in dim1_, d2_ in dim2_, dfix3_) = body__)
ret = :([$f($(body[]), $d1, $d2, $dfix3) for $d1 in $dim1, $d2 in $dim2])
end
#meq (macro with 1 method)
julia> prettify(#macroexpand #meq par1!(d1 in DIM1, d2 in DIM2, dfix3) = par2(d1,d2)+par3(d1,d2))
:([(Main.par1!)((Main.par2)(lobster, redpanda) + (Main.par3)(lobster, redpanda), lobster, redpanda, Main.dfix3) for lobster = Main.DIM1, redpanda = Main.DIM2])
UPDATE:
The desired final expression is a comprehension, it seems that for some reason Julia couldn't figure out for expr(where $expr #=> XXX in XXX) is a comprehension. The workaround is directly using its lowered form:
julia> using MacroTools
julia> par1(a, b, c, d) = a + b + c + d
par1 (generic function with 1 method)
julia> par2(a, b) = a + b
par2 (generic function with 1 method)
julia> macro meq(ex)
#capture(ex, par_(dims__) = rhs_)
loopElements = []
dimsPlaceholders = []
for d in dims
#capture(d, di_ in DIMi_) || (push!(dimsPlaceholders, d); continue)
# push!(loopElements, x)
push!(loopElements, :($di = $DIMi))
push!(dimsPlaceholders, di)
end
ret = Expr(:comprehension, :($par($(rhs),$(dimsPlaceholders...))), loopElements...)
end
#meq (macro with 1 method)
julia> prettify(#macroexpand #meq par1!(d1 in DIM1, d2 in DIM2, dfix3) = par2(d1,d2)+par3(d1,d2))
:($(Expr(:comprehension, :((Main.par1!)(begin
(Main.par2)(bee, wildebeest) + (Main.par3)(bee, wildebeest)
end, bee, wildebeest, Main.dfix3)), :(bee = Main.DIM1), :(wildebeest = Main.DIM2))))
julia> #meq par1(m in 1:2, n in 4:5, 3) = par2(m,n) + par2(m,n)
2×2 Array{Int64,2}:
18 21
21 24
Note that, the variable scope of d1,d2 in generated expression will be wrong if we use push!(loopElements, x) rather than push!(loopElements, :($di = $DIMi)). Let's wait for someone knowledgeable to give a thorough explanation.
If you do not want to rely on an external package for this, the solution I provided on the Julia discourse should also work
return :([$(Expr(:generator,:($(Expr(:call,lhs_par,rhs,lhs_dims_placeholders...))),loop_wholeElements...))])
The key is to use the :generator constructor to make the loop expression
Also, rhs can be replaced with rhs.args[n] in order to eliminate the quote block and insert the expression directly.
This then produces the exact expression:
:([(par1!(par2(d1, d2) + par3(d1, d2), d1, d2, dfix3) for d1 in DIM1, d2 in DIM2)])
EDIT:
Alright, so I went ahead and tested this:
return Expr(:comprehension,Expr(:generator,Expr(:call,lhs_par,rhs.args[2],lhs_dims_placeholders...),loop_wholeElements...))
end
Then computing the result like this
meq(:(par1!(d1 = 1:2, d2 = 1:2, 3) = par2(d1,d2)+par3(d1,d2))) |> eval

Getting error from: dlen = uint32(0) ;

I don't know why but I am getting this error:
Error in mr_lsbpex (line 3)
dlen = uint32(0) ;
Output argument "a" (and maybe others) not assigned during call to "E:\path\mr_lsbpex.m>mr_lsbpex"
I have tested "dlen = uint32(0) ;" in matlab enviorment (outside of this function) and everything was OK. Here is my code:
function a = mr_lsbpex ( r, p )
% extract from an array
dlen = uint32(0) ;
s = size (r) ;
rnd = rand (s(1),s(2)) ;
rd = 32 ;
rl = s(2) ;
for i=1:s(2)
if rnd(1,i)<rd/rl
d = bitget (round(r(1,i)/p),1);
dlen = bitset (dlen,rd,d);
rd = rd -1 ;
end
rl = rl -1 ;
end
if (dlen > 10000000 )
clear a ;
return ;
end
a = uint8(zeros(dlen,1)) ;
rd = double(dlen * 8) ;
rl = double(s(1)*s(2)-s(2)) ;
for i=2:s(1)
for j=1:s(2)
if rnd(i,j)<rd/rl
d = bitget (round(r(i,j)/p) ,1) ;
a = z_set_bit (a,rd,d) ;
rd = rd - 1 ;
end
rl = rl - 1 ;
end
end
Remember: a needs to be returned ALLWAYS!
The error is not in that specific line, but in the "whole" function itself.
Your problem is that Matlab thinks that a its not going to be created. And actually in some case it may not be created.
The following line in the beginning of your function should do the trick
a=0; % well, or a=NaN; or whatever you want to return
Additionally, don't clear a in if (dlen > 10000000 ).

Simulation in verilog using $monitor

I've been trying to implement full adder in Verilog. I have implemented it and it is also showing results on Isim. Only problem is that when I try to see the simulation using $monitor command, it is showing me only 1 result, not all simulation results. Here is testbench code:
module Full_adder_s2_testbench;
// Inputs
reg a;
reg b;
reg cin;
// Outputs
wire sum;
wire cout;
// Instantiate the Unit Under Test (UUT)
Full_adder_s2 uut (
.a(a),
.b(b),
.cin(cin),
.sum(sum),
.cout(cout)
);
integer i;
initial begin
// Initialize Inputs
a = 0;
b = 0;
cin = 0;
// Wait 100 ns for global reset to finish
#100;
end
always # ( a, b, cin )
begin
// generate truth table
for ( i = 0; i < 8; i = i + 1 )
// every 10 ns set a, b, and cin to the binary rep. of i
#10 {a, b, cin} = i;
$monitor( "%d ns: a + b + cin = %b + %b + %b = cout sum = %b %b",
$time, a, b, cin, cout, sum );
// stop 10ns after last change of inputs
#10 $stop;
end
endmodule
And here is result in ISIM:
# run 1000 ns
Simulator is doing circuit initialization process.
Finished circuit initialization process.
400 ns: a + b + cin = 1 + 1 + 1 = cout sum = 1 1
Stopped at time : 410 ns : in File "E:/Namal/FYP/My work/XILINX/Full_adder_s2/Full_adder_s2_testbench.v" Line 66
$monitor is only meant to be setup once and will trigger every time a signal changes, try using $display since you already have the statement inside of your always #*.
While learning Verilog I would encourage you to use begin end liberally. The issue is that only 1 line was in the for loop, the $display/$monitor was outside and so only executed once at the start.
always #* begin
// generate truth table
for ( i = 0; i < 8; i = i + 1 ) begin //<-- Added begin
// every 10 ns set a, b, and cin to the binary rep. of i
#10 {a, b, cin} = i;
$display( "%d ns: a + b + cin = %b + %b + %b = cout sum = %b %b", $time, a, b, cin, cout, sum );
end //<--Added end
// stop 10ns after last input
#10 $stop;
end
Full example on EDA Playground.
NB: it is best not to use manual sensitivity lists any more replace always # ( a, b, cin ) with always #*. This will result in quicker refactoring and lowering the chance of RTL to gates simulation mismatch.